forked from ST-Apps/CS-ParallelRoadTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
387 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using ColossalFramework.Globalization; | ||
using ParallelRoadTool; | ||
using ParallelRoadTool.Extensions.LocaleModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace ParallelRoadTool.Extensions | ||
{ | ||
/// <summary> | ||
/// <see cref="https://github.com/markusmitbrille/cities-skylines-custom-namelists/blob/master/CSLCNL/CSLCNL/LocaleManagerExtensions.cs"/> | ||
/// </summary> | ||
static class LocaleManagerExtensions | ||
{ | ||
const string localeFieldName = "m_Locale"; | ||
const string localizedStringsFieldName = "m_LocalizedStrings"; | ||
const string localizedStringsCountFieldName = "m_LocalizedStringsCount"; | ||
|
||
static FieldInfo localeField = typeof(LocaleManager).GetField(localeFieldName, BindingFlags.Instance | BindingFlags.NonPublic); | ||
static FieldInfo localizedStringsField = typeof(Locale).GetField(localizedStringsFieldName, BindingFlags.Instance | BindingFlags.NonPublic); | ||
static FieldInfo localizedStringsCountField = typeof(Locale).GetField(localizedStringsCountFieldName, BindingFlags.Instance | BindingFlags.NonPublic); | ||
|
||
public static Locale GetLocale(this LocaleManager localeManager) | ||
{ | ||
return (Locale)localeField.GetValue(localeManager); | ||
} | ||
|
||
public static Dictionary<Locale.Key, string> GetLocalizedStrings(this Locale locale) | ||
{ | ||
return (Dictionary<Locale.Key, string>)localizedStringsField.GetValue(locale); | ||
} | ||
|
||
public static Dictionary<Locale.Key, int> GetLocalizedStringsCount(this Locale locale) | ||
{ | ||
return (Dictionary<Locale.Key, int>)localizedStringsCountField.GetValue(locale); | ||
} | ||
|
||
public static void RemoveRange(this LocaleManager localeManager, Locale.Key id) | ||
{ | ||
Locale locale = localeManager.GetLocale(); | ||
|
||
// Set index to 0 so we can check for the string count | ||
id.m_Index = 0; | ||
|
||
if (!locale.Exists(id)) | ||
{ | ||
DebugUtils.Log($"Could not remove locale range {id}; localized string {id} does not exist!"); | ||
return; | ||
} | ||
|
||
Dictionary<Locale.Key, string> localizedStrings = locale.GetLocalizedStrings(); | ||
Dictionary<Locale.Key, int> localizedStringsCount = locale.GetLocalizedStringsCount(); | ||
|
||
for (int index = 0, lastIndex = locale.CountUnchecked(id); index <= lastIndex; index++, id.m_Index = index) | ||
{ | ||
localizedStrings.Remove(id); | ||
localizedStringsCount.Remove(id); | ||
} | ||
|
||
DebugUtils.Log($"Removed locale range {id.m_Identifier}[{id.m_Key}]."); | ||
} | ||
|
||
public static void AddString(this LocaleManager localeManager, LocalizedString localizedString) | ||
{ | ||
Locale locale = localeManager.GetLocale(); | ||
|
||
// Construct 0-index id for the localized string from argument | ||
Locale.Key id; | ||
id.m_Identifier = localizedString.Identifier; | ||
id.m_Key = localizedString.Key; | ||
id.m_Index = 0; | ||
|
||
// Check if the id already exists; if so find next index | ||
if (locale.Exists(id)) | ||
{ | ||
// Log message lags game on large namelists | ||
// Log($"Localized string {localizedString.Identifier}[{localizedString.Key}] already exists, adding it with next available index."); | ||
id.m_Index = locale.CountUnchecked(id); | ||
} | ||
|
||
// Add the localized string | ||
locale.AddLocalizedString(id, localizedString.Value); | ||
|
||
// Set the string counts accordingly | ||
Dictionary<Locale.Key, int> localizedStringCounts = locale.GetLocalizedStringsCount(); | ||
|
||
// The count at the exact index appears to always be 0 | ||
localizedStringCounts[id] = 0; | ||
|
||
// index = 0 appears to be a special case and indicates the count of localized strings with the same identifier and key | ||
Locale.Key zeroIndexID = id; | ||
zeroIndexID.m_Index = 0; | ||
localizedStringCounts[zeroIndexID] = id.m_Index + 1; | ||
|
||
// Log message lags game on large namelists | ||
// Log($"Added localized string {id} = '{localizedString.Value}', count = {localizedStringCounts[zeroIndexID]}."); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ParallelRoadTool/Extensions/LocaleModels/LocalizedString.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml.Serialization; | ||
|
||
namespace ParallelRoadTool.Extensions.LocaleModels | ||
{ | ||
/// <summary> | ||
/// <see cref="https://github.com/markusmitbrille/cities-skylines-custom-namelists/blob/master/CSLCNL/CSLCNL/LocalizedString.cs"/> | ||
/// </summary> | ||
public struct LocalizedString | ||
{ | ||
[XmlAttribute(AttributeName = "identifier")] | ||
public string Identifier; | ||
[XmlAttribute(AttributeName = "key")] | ||
public string Key; | ||
[XmlText] | ||
public string Value; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
ParallelRoadTool/Extensions/LocaleModels/LocalizedStringKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml.Serialization; | ||
|
||
namespace ParallelRoadTool.Extensions.LocaleModels | ||
{ | ||
/// <summary> | ||
/// <see cref="https://github.com/markusmitbrille/cities-skylines-custom-namelists/blob/master/CSLCNL/CSLCNL/LocalizedStringKey.cs"/> | ||
/// </summary> | ||
public struct LocalizedStringKey | ||
{ | ||
[XmlAttribute(AttributeName = "identifier")] | ||
public string Identifier; | ||
[XmlAttribute(AttributeName = "key")] | ||
public string Key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using ColossalFramework; | ||
using ColossalFramework.Globalization; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml.Serialization; | ||
|
||
namespace ParallelRoadTool.Extensions.LocaleModels | ||
{ | ||
/// <summary> | ||
/// <see cref="https://github.com/markusmitbrille/cities-skylines-custom-namelists/blob/master/CSLCNL/CSLCNL/NameList.cs"/> | ||
/// </summary> | ||
public class NameList | ||
{ | ||
[XmlAttribute(AttributeName = "name")] | ||
public string Name; | ||
[XmlArray(ElementName = "strings")] | ||
public LocalizedString[] LocalizedStrings; | ||
|
||
public void Apply() | ||
{ | ||
LocaleManager localeManager = SingletonLite<LocaleManager>.instance; | ||
foreach (LocalizedString localizedString in LocalizedStrings) | ||
{ | ||
localeManager.AddString(localizedString); | ||
} | ||
|
||
DebugUtils.Log($"Namelist {Name} applied."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<NameList | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | ||
name="PRT_en"> | ||
<strings> | ||
<LocalizedString identifier="PRT_TOOLTIPS" key="SnappingToggleButton">Toggle snapping for existing nodes</LocalizedString> | ||
<LocalizedString identifier="PRT_TOOLTIPS" key="ToolToggleButton">Toggle Parallel Road Tool</LocalizedString> | ||
<LocalizedString identifier="PRT_TOOLTIPS" key="ReverseToggleButton">Toggle reverse direction for this road</LocalizedString> | ||
<LocalizedString identifier="PRT_TOOLTIPS" key="RemoveNetworkButton">Remove network</LocalizedString> | ||
<LocalizedString identifier="PRT_TOOLTIPS" key="AddNetworkButton">Add network</LocalizedString> | ||
|
||
<LocalizedString identifier="PRT_TEXTS" key="SameAsSelectedLabel">Same as selected</LocalizedString> | ||
<LocalizedString identifier="PRT_TEXTS" key="DecreaseHorizontalOffsetOption">Decrease horizontal offset of parallel networks</LocalizedString> | ||
<LocalizedString identifier="PRT_TEXTS" key="IncreaseHorizontalOffsetOption">Increase horizontal offset of parallel networks</LocalizedString> | ||
<LocalizedString identifier="PRT_TEXTS" key="DecreaseVerticalOffsetOption">Decrease vertical offset of parallel networks</LocalizedString> | ||
<LocalizedString identifier="PRT_TEXTS" key="IncreaseVerticalOffsetOption">Increase vertical offset of parallel networks</LocalizedString> | ||
</strings> | ||
</NameList> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.