private static void ApplyConfiguration(DistrictsConfiguration configuration) { var buildingThemesManager = BuildingThemesManager.instance; buildingThemesManager.ImportThemes(); foreach (var district in configuration.Districts) { //skip districts which do not exist if (DistrictManager.instance.m_districts.m_buffer[district.id].m_flags == District.Flags.None) { continue; } var themes = new HashSet <Configuration.Theme>(); foreach (var themeName in district.themes) { var theme = buildingThemesManager.GetThemeByName(themeName); if (theme == null) { Debugger.LogFormat("Theme {0} that was enabled in district {1} could not be found!", themeName, district.id); continue; } themes.Add(theme); } if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Loading: {0} themes enabled for district {1}", themes.Count, district.id); } buildingThemesManager.setThemeInfo(district.id, themes, district.blacklistMode); } }
public static void Serialize(string filename, DistrictsConfiguration config) { var serializer = new XmlSerializer(typeof(DistrictsConfiguration)); using (var writer = new StreamWriter(filename)) { config.OnPreSerialize(); serializer.Serialize(writer, config); } }
public static void Serialize(string filename, DistrictsConfiguration config) { var serializer = new XmlSerializer(typeof(DistrictsConfiguration)); using (var writer = new StreamWriter(filename)) { config.OnPreSerialize(); serializer.Serialize(writer, config); } }
private static void OnLoadDataTimed(System.Object source, ElapsedEventArgs e) { byte[] data = SerializableData.LoadData(DataId); UniqueId = 0u; for (var i = 0; i < data.Length - 3; i++) { UniqueId = BitConverter.ToUInt32(data, i); } var filepath = BuildSaveFilePath(); _timer.Enabled = false; if (!File.Exists(filepath)) { return; } var configuration = DistrictsConfiguration.Deserialize(filepath); var buildingThemesManager = Singleton <BuildingThemesManager> .instance; foreach (var district in configuration.Districts) { var themes = new HashSet <Configuration.Theme>(); foreach (var themeName in district.themes) { var theme = buildingThemesManager.GetThemeByName(themeName); if (theme == null) { continue; } themes.Add(theme); } if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Loading: {0} themes enabled for district {1}", themes.Count, district.id); } buildingThemesManager.setThemeInfo(district.id, themes, district.blacklistMode); } }
public void OnSaveData() { if (Debugger.Enabled) { Debugger.Log("Building Themes: SerializableDataExtension.OnSaveData was called."); Debugger.Log("ON_SAVE_DATA"); } var data = new FastList <byte>(); GenerateUniqueId(); var uniqueIdBytes = BitConverter.GetBytes(UniqueId); foreach (var uniqueIdByte in uniqueIdBytes) { data.Add(uniqueIdByte); } var dataToSave = data.ToArray(); SerializableData.SaveData(DataId, dataToSave); var filepath = BuildSaveFilePath(); var configuration = new DistrictsConfiguration(); var themesManager = Singleton <BuildingThemesManager> .instance; for (byte i = 0; i < 128; i++) { if (!themesManager.IsThemeManagementEnabled(i)) { continue; } var themes = themesManager.GetDistrictThemes(i, false); if (themes == null) { continue;; } var themesNames = new string[themes.Count]; var j = 0; foreach (var theme in themes) { themesNames[j] = theme.name; j++; } configuration.Districts.Add(new DistrictsConfiguration.District() { id = i, blacklistMode = themesManager.IsBlacklistModeEnabled(i), themes = themesNames }); if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Saving: {0} themes enabled for district {1}", themes.Count, i); } } if (configuration.Districts.Count > 0) { DistrictsConfiguration.Serialize(filepath, configuration); } if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Serialization done."); Debugger.AppendThemeList(); } }
private static void ApplyConfiguration(DistrictsConfiguration configuration) { var buildingThemesManager = BuildingThemesManager.instance; buildingThemesManager.ImportThemes(); foreach (var district in configuration.Districts) { //skip districts which do not exist if (DistrictManager.instance.m_districts.m_buffer[district.id].m_flags == District.Flags.None) continue; var themes = new HashSet<Configuration.Theme>(); foreach (var themeName in district.themes) { var theme = buildingThemesManager.GetThemeByName(themeName); if (theme == null) { Debugger.LogFormat("Theme {0} that was enabled in district {1} could not be found!", themeName, district.id); continue; } themes.Add(theme); } if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Loading: {0} themes enabled for district {1}", themes.Count, district.id); } buildingThemesManager.setThemeInfo(district.id, themes, district.blacklistMode); } }
public void OnSaveData() { if (Debugger.Enabled) { Debugger.Log("Building Themes: Saving Data..."); } try { var configuration = new DistrictsConfiguration(); var themesManager = Singleton<BuildingThemesManager>.instance; for (byte i = 0; i < 128; i++) { if (!themesManager.IsThemeManagementEnabled(i)) continue; var themes = themesManager.GetDistrictThemes(i, false); if (themes == null) { continue; ; } var themesNames = new string[themes.Count]; var j = 0; foreach (var theme in themes) { themesNames[j] = theme.name; j++; } configuration.Districts.Add(new DistrictsConfiguration.District() { id = i, blacklistMode = themesManager.IsBlacklistModeEnabled(i), themes = themesNames }); if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Saving: {0} themes enabled for district {1}", themes.Count, i); } } if (configuration.Districts.Count > 0) { byte[] configurationData; var xmlSerializer = new XmlSerializer(typeof(DistrictsConfiguration)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); using (var memoryStream = new MemoryStream()) { xmlSerializer.Serialize(memoryStream, configuration, ns); configurationData = memoryStream.ToArray(); } SerializableData.SaveData(XMLSaveDataId, configurationData); // output for debugging /* using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter("BuildingThemesData.xml")) { xmlSerializer.Serialize(streamWriter, configuration, ns); } */ } if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Serialization done."); Debugger.AppendThemeList(); } } catch (Exception ex) { Debugger.LogError("Building Themes: Error saving theme data"); Debugger.LogException(ex); } }
public void OnSaveData() { if (Debugger.Enabled) { Debugger.Log("Building Themes: SerializableDataExtension.OnSaveData was called."); Debugger.Log("ON_SAVE_DATA"); } var data = new FastList<byte>(); GenerateUniqueId(); var uniqueIdBytes = BitConverter.GetBytes(UniqueId); foreach (var uniqueIdByte in uniqueIdBytes) { data.Add(uniqueIdByte); } var dataToSave = data.ToArray(); SerializableData.SaveData(DataId, dataToSave); var filepath = BuildSaveFilePath(); var configuration = new DistrictsConfiguration(); var themesManager = Singleton<BuildingThemesManager>.instance; for (byte i = 0; i < 128; i++) { if (!themesManager.IsThemeManagementEnabled(i)) continue; var themes = themesManager.GetDistrictThemes(i, false); if (themes == null) { continue; ; } var themesNames = new string[themes.Count]; var j = 0; foreach (var theme in themes) { themesNames[j] = theme.name; j++; } configuration.Districts.Add(new DistrictsConfiguration.District() { id = i, blacklistMode = themesManager.IsBlacklistModeEnabled(i), themes = themesNames }); if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Saving: {0} themes enabled for district {1}", themes.Count, i); } } if (configuration.Districts.Count > 0) DistrictsConfiguration.Serialize(filepath, configuration); if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Serialization done."); Debugger.AppendThemeList(); } }
public void OnLoadData() { try { byte[] saveData = SerializableData.LoadData(XMLSaveDataId); if (saveData != null) { if (Debugger.Enabled) { Debugger.Log("Building Themes: Loading Save Data..."); } DistrictsConfiguration configuration = null; var xmlSerializer = new XmlSerializer(typeof(DistrictsConfiguration)); using (var memoryStream = new MemoryStream(saveData)) { configuration = xmlSerializer.Deserialize(new MemoryStream(saveData)) as DistrictsConfiguration; } ApplyConfiguration(configuration); } else { // search for legacy save data byte[] legacyData = SerializableData.LoadData(LegacyDataId); if (legacyData != null) { if (Debugger.Enabled) { Debugger.Log("Building Themes: Loading Legacy Save Data..."); } var UniqueId = 0u; for (var i = 0; i < legacyData.Length - 3; i++) { UniqueId = BitConverter.ToUInt32(legacyData, i); } Debug.Log(UniqueId); var filepath = Path.Combine(Application.dataPath, String.Format("buildingThemesSave_{0}.xml", UniqueId)); Debug.Log(filepath); if (!File.Exists(filepath)) { if (Debugger.Enabled) { Debugger.Log(filepath + " not found!"); } return; } DistrictsConfiguration configuration; var serializer = new XmlSerializer(typeof(DistrictsConfiguration)); try { using (var reader = new StreamReader(filepath)) { configuration = (DistrictsConfiguration)serializer.Deserialize(reader); } } catch { configuration = null; } ApplyConfiguration(configuration); } else { if (Debugger.Enabled) { Debugger.Log("No legacy save data found!"); } } } } catch (Exception ex) { Debugger.LogError("Building Themes: Error loading theme data"); Debugger.LogException(ex); } }
public void OnSaveData() { if (Debugger.Enabled) { Debugger.Log("Building Themes: Saving Data..."); } try { var configuration = new DistrictsConfiguration(); var themesManager = Singleton <BuildingThemesManager> .instance; for (byte i = 0; i < 128; i++) { if (!themesManager.IsThemeManagementEnabled(i)) { continue; } var themes = themesManager.GetDistrictThemes(i, false); if (themes == null) { continue;; } var themesNames = new string[themes.Count]; var j = 0; foreach (var theme in themes) { themesNames[j] = theme.name; j++; } configuration.Districts.Add(new DistrictsConfiguration.District() { id = i, blacklistMode = themesManager.IsBlacklistModeEnabled(i), themes = themesNames }); if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Saving: {0} themes enabled for district {1}", themes.Count, i); } } if (configuration.Districts.Count > 0) { byte[] configurationData; var xmlSerializer = new XmlSerializer(typeof(DistrictsConfiguration)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); using (var memoryStream = new MemoryStream()) { xmlSerializer.Serialize(memoryStream, configuration, ns); configurationData = memoryStream.ToArray(); } SerializableData.SaveData(XMLSaveDataId, configurationData); // output for debugging /* * using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter("BuildingThemesData.xml")) * { * xmlSerializer.Serialize(streamWriter, configuration, ns); * } */ } if (Debugger.Enabled) { Debugger.LogFormat("Building Themes: Serialization done."); Debugger.AppendThemeList(); } } catch (Exception ex) { Debugger.LogError("Building Themes: Error saving theme data"); Debugger.LogException(ex); } }