这份代码主要工作是将items_game.txt里面的数据,解析成Json格式
#pragma newdecls required
public void OnPluginStart()
{
char path[PLATFORM_MAX_PATH];
BuildPath(Path_SM, path, sizeof(path), "../../scripts/items/items_game.txt");
char jsonpath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, jsonpath, sizeof(jsonpath), "../../scripts/items/items_game.json");
char last[4096], curr[4096];
char explodeString[3][4096];
int strings;
File file = OpenFile(path, "r");
File json = OpenFile(jsonpath, "w");
file.ReadLine(curr, sizeof(curr));
while (!file.EndOfFile()) {
file.ReadLine(curr, sizeof(curr));
TrimString(curr);
if (strcmp(curr, "{") == 0) {
if (strlen(last) > 0) json.WriteLine("%s:", last);
} else if (strcmp(curr, "}") == 0) {
if (strings == 3) json.WriteLine("%s: %s", explodeString[0], explodeString[2]);
else json.WriteLine("%s", last);
} else {
if (strings == 3) json.WriteLine("%s: %s,", explodeString[0], explodeString[2]);
else {
if (strcmp(last, "}") == 0) json.WriteLine("},");
else json.WriteLine("%s", last);
}
}
strings = ExplodeString(curr, "\t", explodeString, sizeof(explodeString), 4096);
strcopy(last, sizeof(last), curr);
}
delete file;
delete json;
}
参考:[CS:GO] CSGOItemsJSON - AlliedModders (alliedmods.net)