Skip to content

Commit

Permalink
在非运行模式,也可以进行配置表重载,供非技术人员,来检查是否有填表错误引发异常
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-kelly committed Apr 15, 2016
1 parent 5dca8b9 commit 72dae71
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 86 deletions.
205 changes: 137 additions & 68 deletions KEngine.UnityProject/Assets/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ public static IReloadableSettings[] SettingsList
return _settingsList;
}
}
#if UNITY_EDITOR
static bool HasAllReload = false;
[UnityEditor.MenuItem("KEngine/Settings/Try Reload All Settings Code")]
public static void AllSettingsReload()
{
for (var i = 0; i < SettingsList.Length; i++)
{
var settings = SettingsList[i];
if (HasAllReload) settings.ReloadAll();
HasAllReload = true;

KLogger.Log("Reload settings: {0}, Row Count: {1}", settings.GetType(), settings.Count);

}
}

#endif
}


Expand All @@ -66,7 +83,7 @@ public static IReloadableSettings[] SettingsList
public partial class ExampleSettings : IReloadableSettings
{
public static readonly string TabFilePath = "Example.bytes";
static readonly ExampleSettings _instance = new ExampleSettings();
static ExampleSettings _instance;
Dictionary<string, ExampleSetting> _dict = new Dictionary<string, ExampleSetting>();

/// <summary>
Expand All @@ -80,20 +97,6 @@ public partial class ExampleSettings : IReloadableSettings
/// </summary>
private ExampleSettings()
{
ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}

/// <summary>
Expand All @@ -102,8 +105,35 @@ private ExampleSettings()
/// <returns></returns>
public static ExampleSettings GetInstance()
{
if (_instance == null)
{
_instance = new ExampleSettings();

_instance.ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
_instance.ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}
return _instance;
}

public int Count
{
get
{
return _dict.Count;
}
}

/// <summary>
/// Do reload the setting file
Expand Down Expand Up @@ -133,7 +163,7 @@ public void ReloadAll()

public static IEnumerable GetAll()
{
foreach (var row in _instance._dict.Values)
foreach (var row in GetInstance()._dict.Values)
{
yield return row;
}
Expand All @@ -142,7 +172,7 @@ public static IEnumerable GetAll()
public static ExampleSetting GetByPrimaryKey(string primaryKey)
{
ExampleSetting setting;
if (_instance._dict.TryGetValue(primaryKey, out setting)) return setting;
if (GetInstance()._dict.TryGetValue(primaryKey, out setting)) return setting;
return null;
}
}
Expand Down Expand Up @@ -219,7 +249,7 @@ public static string ParsePrimaryKey(TableRow row)
public partial class SubdirExample2Settings : IReloadableSettings
{
public static readonly string TabFilePath = "Subdir/Example2.bytes";
static readonly SubdirExample2Settings _instance = new SubdirExample2Settings();
static SubdirExample2Settings _instance;
Dictionary<int, SubdirExample2Setting> _dict = new Dictionary<int, SubdirExample2Setting>();

/// <summary>
Expand All @@ -233,20 +263,6 @@ public partial class SubdirExample2Settings : IReloadableSettings
/// </summary>
private SubdirExample2Settings()
{
ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}

/// <summary>
Expand All @@ -255,8 +271,35 @@ private SubdirExample2Settings()
/// <returns></returns>
public static SubdirExample2Settings GetInstance()
{
if (_instance == null)
{
_instance = new SubdirExample2Settings();

_instance.ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
_instance.ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}
return _instance;
}

public int Count
{
get
{
return _dict.Count;
}
}

/// <summary>
/// Do reload the setting file
Expand Down Expand Up @@ -286,7 +329,7 @@ public void ReloadAll()

public static IEnumerable GetAll()
{
foreach (var row in _instance._dict.Values)
foreach (var row in GetInstance()._dict.Values)
{
yield return row;
}
Expand All @@ -295,7 +338,7 @@ public static IEnumerable GetAll()
public static SubdirExample2Setting GetByPrimaryKey(int primaryKey)
{
SubdirExample2Setting setting;
if (_instance._dict.TryGetValue(primaryKey, out setting)) return setting;
if (GetInstance()._dict.TryGetValue(primaryKey, out setting)) return setting;
return null;
}
}
Expand Down Expand Up @@ -348,7 +391,7 @@ public static int ParsePrimaryKey(TableRow row)
public partial class SubdirSettings : IReloadableSettings
{
public static readonly string TabFilePath = "Subdir/__.bytes";
static readonly SubdirSettings _instance = new SubdirSettings();
static SubdirSettings _instance;
Dictionary<string, SubdirSetting> _dict = new Dictionary<string, SubdirSetting>();

/// <summary>
Expand All @@ -362,20 +405,6 @@ public partial class SubdirSettings : IReloadableSettings
/// </summary>
private SubdirSettings()
{
ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}

/// <summary>
Expand All @@ -384,8 +413,35 @@ private SubdirSettings()
/// <returns></returns>
public static SubdirSettings GetInstance()
{
if (_instance == null)
{
_instance = new SubdirSettings();

_instance.ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
_instance.ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}
return _instance;
}

public int Count
{
get
{
return _dict.Count;
}
}

/// <summary>
/// Do reload the setting file
Expand Down Expand Up @@ -415,7 +471,7 @@ public void ReloadAll()

public static IEnumerable GetAll()
{
foreach (var row in _instance._dict.Values)
foreach (var row in GetInstance()._dict.Values)
{
yield return row;
}
Expand All @@ -424,7 +480,7 @@ public static IEnumerable GetAll()
public static SubdirSetting GetByPrimaryKey(string primaryKey)
{
SubdirSetting setting;
if (_instance._dict.TryGetValue(primaryKey, out setting)) return setting;
if (GetInstance()._dict.TryGetValue(primaryKey, out setting)) return setting;
return null;
}
}
Expand Down Expand Up @@ -477,7 +533,7 @@ public static string ParsePrimaryKey(TableRow row)
public partial class SubdirSubSubDirExample3Settings : IReloadableSettings
{
public static readonly string TabFilePath = "Subdir/SubSubDir/Example3.bytes";
static readonly SubdirSubSubDirExample3Settings _instance = new SubdirSubSubDirExample3Settings();
static SubdirSubSubDirExample3Settings _instance;
Dictionary<string, SubdirSubSubDirExample3Setting> _dict = new Dictionary<string, SubdirSubSubDirExample3Setting>();

/// <summary>
Expand All @@ -491,20 +547,6 @@ public partial class SubdirSubSubDirExample3Settings : IReloadableSettings
/// </summary>
private SubdirSubSubDirExample3Settings()
{
ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}

/// <summary>
Expand All @@ -513,8 +555,35 @@ private SubdirSubSubDirExample3Settings()
/// <returns></returns>
public static SubdirSubSubDirExample3Settings GetInstance()
{
if (_instance == null)
{
_instance = new SubdirSubSubDirExample3Settings();

_instance.ReloadAll();
#if UNITY_EDITOR
if (SettingModule.IsFileSystemMode)
{
SettingModule.WatchSetting(TabFilePath, (path) =>
{
if (path.Replace("\\", "/").EndsWith(path))
{
_instance.ReloadAll();
KLogger.LogConsole_MultiThread("Reload success! -> " + path);
}
});
}
#endif
}
return _instance;
}

public int Count
{
get
{
return _dict.Count;
}
}

/// <summary>
/// Do reload the setting file
Expand Down Expand Up @@ -544,7 +613,7 @@ public void ReloadAll()

public static IEnumerable GetAll()
{
foreach (var row in _instance._dict.Values)
foreach (var row in GetInstance()._dict.Values)
{
yield return row;
}
Expand All @@ -553,7 +622,7 @@ public static IEnumerable GetAll()
public static SubdirSubSubDirExample3Setting GetByPrimaryKey(string primaryKey)
{
SubdirSubSubDirExample3Setting setting;
if (_instance._dict.TryGetValue(primaryKey, out setting)) return setting;
if (GetInstance()._dict.TryGetValue(primaryKey, out setting)) return setting;
return null;
}
}
Expand Down
Loading

0 comments on commit 72dae71

Please sign in to comment.