/// <summary> /// Saves the given MapDataBase to disk. /// </summary> private void SaveData(WorldSavedData par1WorldSavedData) { if (SaveHandler == null) { return; } try { string file = SaveHandler.GetMapFileFromName(par1WorldSavedData.MapName); if (file != null) { NBTTagCompound nbttagcompound = new NBTTagCompound(); par1WorldSavedData.WriteToNBT(nbttagcompound); NBTTagCompound nbttagcompound1 = new NBTTagCompound(); nbttagcompound1.SetCompoundTag("data", nbttagcompound); FileStream fileoutputstream = new FileStream(file, FileMode.Create); CompressedStreamTools.WriteCompressed(nbttagcompound1, fileoutputstream); fileoutputstream.Close(); } } catch (Exception exception) { Utilities.LogException(exception); } }
/// <summary> /// Saves all dirty loaded MapDataBases to disk. /// </summary> public virtual void SaveAllData() { for (int i = 0; i < LoadedDataList.Count; i++) { WorldSavedData worldsaveddata = (WorldSavedData)LoadedDataList[i]; if (worldsaveddata.IsDirty()) { SaveData(worldsaveddata); worldsaveddata.SetDirty(false); } } }
/// <summary> /// Assigns the given String id to the given MapDataBase, removing any existing ones of the same id. /// </summary> public virtual void SetData(string par1Str, WorldSavedData par2WorldSavedData) { if (par2WorldSavedData == null) { throw new Exception("Can't set null data"); } if (LoadedDataMap.ContainsKey(par1Str)) { LoadedDataList.Remove(LoadedDataMap[par1Str]); LoadedDataMap.Remove(par1Str); } LoadedDataMap[par1Str] = par2WorldSavedData; LoadedDataList.Add(par2WorldSavedData); }
/// <summary> /// Loads an existing MapDataBase corresponding to the given String id from disk, instantiating the given Class, or /// returns null if none such file exists. args: Class to instantiate, String dataid /// </summary> public virtual WorldSavedData LoadData(Type par1Class, string par2Str) { WorldSavedData worldsaveddata = LoadedDataMap[par2Str]; if (worldsaveddata != null) { return(worldsaveddata); } if (SaveHandler != null) { try { string file = SaveHandler.GetMapFileFromName(par2Str); if (file != null && File.Exists(file)) { try { worldsaveddata = (WorldSavedData)Activator.CreateInstance(par1Class, new object[] { par2Str }); } catch (Exception exception1) { throw new Exception(new StringBuilder().Append("Failed to instantiate ").Append(par1Class.ToString()).ToString(), exception1); } FileStream fileinputstream = new FileStream(file, FileMode.Open); NBTTagCompound nbttagcompound = CompressedStreamTools.ReadCompressed(fileinputstream); fileinputstream.Close(); worldsaveddata.ReadFromNBT(nbttagcompound.GetCompoundTag("data")); } } catch (Exception exception) { Utilities.LogException(exception); } } if (worldsaveddata != null) { LoadedDataMap[par2Str] = worldsaveddata; LoadedDataList.Add(worldsaveddata); } return(worldsaveddata); }