static public void Write(gameinfo info, string path) { DataContractJsonSerializer j = new DataContractJsonSerializer(typeof(gameinfo)); FileStream fs = new FileStream(path, FileMode.Create); j.WriteObject(fs, info); fs.Close(); }
static public gameinfo Read(string path) { try { gameinfo info; StreamReader JsonFile = new StreamReader(path); DataContractJsonSerializer InfoReader = new DataContractJsonSerializer(typeof(gameinfo)); info = InfoReader.ReadObject(JsonFile.BaseStream) as gameinfo; JsonFile.Close(); if (info.inheritsFrom == "" || info.inheritsFrom == null) { return(info); } String anotherJson = GetGameInfoJsonPath(info.inheritsFrom); gameinfo anotherGameinfo = Read(anotherJson); info.type = (info.type == null || info.type == "") ? anotherGameinfo.type : info.type; //Type:覆盖 info.minecraftArguments = (info.minecraftArguments == null || info.minecraftArguments == "") ? anotherGameinfo.minecraftArguments : info.minecraftArguments; //MinecraftArguments:覆盖 info.mainClass = (info.mainClass == null || info.mainClass == "") ? anotherGameinfo.mainClass : info.mainClass; //MainClass:覆盖 info.libraries = MixLibraries(info.libraries, anotherGameinfo.libraries); //Libraries:拼接 info.assets = (info.assets == null || info.assets == "") ? anotherGameinfo.assets : info.assets; //Assets:覆盖 info.jar = (info.jar == null || info.jar == "") ? anotherGameinfo.jar : info.jar; //Jar:覆盖 return(info); } catch (SerializationException ex) { Logger.Log(ex); try { StreamReader JsonFile = new StreamReader(path, Encoding.Default); DataContractJsonSerializer InfoReader = new DataContractJsonSerializer(typeof(gameinfo)); gameinfo info = InfoReader.ReadObject(JsonFile.BaseStream) as gameinfo; JsonFile.Close(); Logger.Log("JSON文件使用", Encoding.Default.EncodingName, "解析成功,将转换为UTF8编码"); JsonFile = new StreamReader(path, Encoding.Default); string JsonString = JsonFile.ReadToEnd(); JsonFile.Close(); StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8); sw.WriteLine(JsonString); sw.Close(); Logger.Log("JSON文件转存完毕"); if (info.inheritsFrom == "" || info.inheritsFrom == null) { return(info); } String anotherJson = GetGameInfoJsonPath(info.inheritsFrom); gameinfo anotherGameinfo = Read(anotherJson); info.libraries = MixLibraries(info.libraries, anotherGameinfo.libraries); return(info); } catch (SerializationException ex1) { Logger.Log(ex1); return(null); } } }
static public gameinfo Read(string path) { try { gameinfo info; StreamReader JsonFile = new StreamReader(path); DataContractJsonSerializer InfoReader = new DataContractJsonSerializer(typeof(gameinfo)); info = InfoReader.ReadObject(JsonFile.BaseStream) as gameinfo; JsonFile.Close(); return(info); } catch (SerializationException ex) { Logger.log(ex); try { StreamReader JsonFile = new StreamReader(path, Encoding.Default); DataContractJsonSerializer InfoReader = new DataContractJsonSerializer(typeof(gameinfo)); gameinfo info = InfoReader.ReadObject(JsonFile.BaseStream) as gameinfo; JsonFile.Close(); Logger.log("JSON文件使用", Encoding.Default.EncodingName, "解析成功,将转换为UTF8编码"); JsonFile = new StreamReader(path, Encoding.Default); string JsonString = JsonFile.ReadToEnd(); JsonFile.Close(); StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8); sw.WriteLine(JsonString); sw.Close(); Logger.log("JSON文件转存完毕"); return(info); } catch (SerializationException ex1) { Logger.log(ex1); return(null); } } }
public static void Write(gameinfo info,string path) { DataContractJsonSerializer j = new DataContractJsonSerializer(typeof(gameinfo)); FileStream fs = new FileStream(path, FileMode.Create); j.WriteObject(fs, info); fs.Close(); }