DayZ 1.24
Loading...
Searching...
No Matches
JsonFileLoader.c
Go to the documentation of this file.
2{
3 protected static ref JsonSerializer m_Serializer = new JsonSerializer();
4
5 static bool LoadFile(string filename, out T data, out string errorMessage)
6 {
8 {
9 FileHandle handle = OpenFile(filename, FileMode.READ);
10 if (handle == 0)
11 {
12 errorMessage = string.Format("Cannot open file \"%1\" for reading", filename);
13 return false;
14 }
15
16 string fileContent;
17 string lineContent;
18 while (FGets(handle, lineContent) >= 0)
20
21 CloseFile(handle);
22
23 if (!m_Serializer)
24 m_Serializer = new JsonSerializer();
25
26 string error;
27 if (!m_Serializer.ReadFromString(data, fileContent, error))
28 {
29 errorMessage = string.Format("Cannot load data from \"%1\":\n%2", filename, error);
30 return false;
31 }
32
33 return true;
34 }
35 else
36 {
37 errorMessage = string.Format("File \"%1\" does not exist, check the provided path", filename, error);
38 return false;
39 }
40 }
41
42 static bool SaveFile(string filename, T data, out string errorMessage)
43 {
44 string fileContent;
45
46 if (!m_Serializer)
47 m_Serializer = new JsonSerializer();
48
49 string makeDataError;
50 if (!MakeData(data, fileContent, makeDataError, true))
51 {
52 errorMessage = string.Format("Cannot save data to file \"%1\", %2", filename, makeDataError);
53 return false;
54 }
55
56 FileHandle handle = OpenFile(filename, FileMode.WRITE);
57 if (handle == 0)
58 {
59 errorMessage = string.Format("Cannot open file \"%1\" for writing", filename);
60 return false;
61 }
62
63 FPrint(handle, fileContent);
64 CloseFile(handle);
65
66 return true;
67 }
68
69 static bool LoadData(string string_data, out T data, out string errorMessage)
70 {
71 string error;
72
73 if (!m_Serializer)
74 m_Serializer = new JsonSerializer();
75
76 if (!m_Serializer.ReadFromString(data, string_data, error))
77 {
78 errorMessage = string.Format("Cannot load provided JSON data (deserialization error) - check syntax: %1", error);
79 return false;
80 }
81
82 return true;
83 }
84
85 static bool MakeData(T inputData, out string outputData, out string errorMessage, bool prettyPrint = true)
86 {
87 if (!m_Serializer)
88 m_Serializer = new JsonSerializer();
89
90 if (!m_Serializer.WriteToString(inputData, prettyPrint, outputData))
91 {
92 errorMessage = "Cannot create JSON from provided data (serialization error)";
93 return false;
94 }
95
96 return true;
97 }
98
101
103
105 static void JsonLoadFile(string filename, out T data)
106 {
107
108 if (FileExist(filename))
109 {
110 string file_content;
111 string line_content;
112 string error;
113
114 FileHandle handle = OpenFile(filename, FileMode.READ);
115 if (handle == 0)
116 return;
117
118 while (FGets(handle, line_content) >= 0)
120
121 CloseFile(handle);
122
123 if (!m_Serializer)
124 m_Serializer = new JsonSerializer();
125
126 if (!m_Serializer.ReadFromString(data, file_content, error))
127 ErrorEx(string.Format("Cannot load data from \"%1\":\n%2", filename, error));
128 }
129 }
130
132 static void JsonSaveFile(string filename, T data)
133 {
134 string file_content;
135 if (!m_Serializer)
136 m_Serializer = new JsonSerializer();
137
138 m_Serializer.WriteToString(data, true, file_content);
139
140 FileHandle handle = OpenFile(filename, FileMode.WRITE);
141 if (handle == 0)
142 return;
143
144 FPrint(handle, file_content);
145 CloseFile(handle);
146 }
147
149 static void JsonLoadData(string string_data, out T data)
150 {
151 string error;
152 if (!m_Serializer)
153 m_Serializer = new JsonSerializer();
154
155 if (!m_Serializer.ReadFromString(data, string_data, error))
156 ErrorEx(string.Format("Cannot load data %1", error));
157 }
158
160 static string JsonMakeData(T data)
161 {
162 string string_data;
163 if (!m_Serializer)
164 m_Serializer = new JsonSerializer();
165
166 m_Serializer.WriteToString(data, true, string_data);
167 return string_data;
168 }
169
170 //#endif
171}
Super root of all classes in Enforce script.
Definition EnScript.c:11
static bool LoadFile(string filename, out T data, out string errorMessage)
static void JsonLoadFile(string filename, out T data)
#ifndef DIAG_DEVELOPER
static void JsonLoadData(string string_data, out T data)
use JsonFileLoader::LoadData instead
static bool SaveFile(string filename, T data, out string errorMessage)
static bool MakeData(T inputData, out string outputData, out string errorMessage, bool prettyPrint=true)
static void JsonSaveFile(string filename, T data)
use JsonFileLoader::SaveFile instead
static bool LoadData(string string_data, out T data, out string errorMessage)
static string JsonMakeData(T data)
use JsonFileLoader::MakeData instead
Class for sending RPC over network.
Definition gameplay.c:50
enum ShapeType ErrorEx
FileMode
Definition EnSystem.c:383
proto void CloseFile(FileHandle file)
Close the File.
proto void FPrint(FileHandle file, void var)
Write to file.
proto int FGets(FileHandle file, string var)
Get line from file, every next call of this function returns next line.
proto FileHandle OpenFile(string name, FileMode mode)
Opens File.
proto bool FileExist(string name)
Check existence of file.