//c'tors: /// <summary> /// The GuiConfig is not connected (the App is null). As soon as you set the /// app (<see cref="CliConfig.App"/>) the GuiConfig will try to connect with /// the app. /// </summary> public CliConfig() { //mappers mappers = new NamedObjects <IDecimalMapper>(new string[] { "linear", "lin" }, new LinearMapper()); mappers.Register(new string[] { "logarithmic", "log" }, new LogarithmicMapper()); mappers.Register(new string[] { "sqrt", "root" }, new SqrtMapper()); mappers.Register(new string[] { "custom", "points" }, new CustomMapper(new decimal[] { 0m, 0.1m, 0.25m, 0.45m, 0.75m, 1m })); //scales ThresholdScale m1 = new ThresholdScale( new byte[] { 230, 200, 180, 160, 130, 100, 70, 50 }, new char[] { ' ', '.', '*', ':', 'o', '&', '8', '#', '@' }, new Uri("https://www.codeproject.com/Articles/20435/Using-C-To-Generate-ASCII-Art-From-An-Image") ); ThresholdScale m2 = new ThresholdScale( new decimal[] { 0.90197m, 0.8m, 0.70197m, 0.6m, 0.50197m, 0.4m, 0.30197m, 0.2m, 0.10197m }, new char[] { ' ', '.', '-', ':', '*', '+', '=', '%', '@', '#' }, new Uri("https://www.c-sharpcorner.com/article/generating-ascii-art-from-an-image-using-C-Sharp/") ); AutomaticScale m3 = new AutomaticScale( "#@%=+*:-. ", new Uri("https://www.c-sharpcorner.com/article/generating-ascii-art-from-an-image-using-C-Sharp/") ); scales = new NamedObjects <IAsciiArtScale>(new string[] { "codeproject", "default", "standard" }, m1); scales.Register(new string[] { "c-sharpcorner", "alternative" }, m2); scales.Register(new string[] { "auto-example", "auto", "automatic" }, m3); //items items.Add(new Item("TopLeftX", "top left corner, x-coordinate")); items.Add(new Item("TopLeftY", "top left corner, y-coordinate")); items.Add(new Item("AlifeText", "typically only 1 character like 'X'")); items.Add(new Item("DeadText", "typically only 1 character like '-'")); items.Add(new Item("HalfAlifeText", "typically only 1 character like '~'")); items.Add(new Item("AlifeColor", "color for the character 'X'")); items.Add(new Item("DeadColor", "color for the character '-'")); items.Add(new Item("HalfAlifeColor", "color for the character '~'")); items.Add(new Item("RunningText", "typically a text like 'RUNNING...'")); items.Add(new Item("StoppedText", "typically a text like 'STOPPED.'")); items.Add(new Item("RunningColor", "color for the text 'RUNNING...'")); items.Add(new Item("StoppedColor", "color for the text 'STOPPED.'")); items.Add(new Item("DelayMilliSeconds", "number (1 ms = 0.001 seconds)")); items.Add(new Item("FeedbackColorOkay", "color for successful commands")); items.Add(new Item("FeedbackColorError", "color for failed commands")); items.Add(new Item("GenerationText", "a code like: 'gen: {0:#,0}'")); items.Add(new Item("GenerationTextCulture", "how are numbers written?")); items.Add(new Item("GenerationTextColor", "color for 'GenerationText'")); items.Add(new Item("AverageBarText", "a code like 'avg = {0:0.000}'")); items.Add(new Item("AverageBarColor", "color for 'AverageBarText'")); items.Add(new Item("PromptText", "a prompt for user input like '> '")); items.Add(new Item("PromptColor", "color of that prompt '> '")); items.Add(new Item("HelpColor", "color help text (with 'help' or '?')")); items.Add(new Item("BackColor", "general background color for console")); items.Add(new Item("FlagsEnum", "")); //maybe we should move this to a test package //items.Add(new Item("Mappers", "mapper-functions (numbers more visible)")); //items.Add(new Item("Scales", "number-to-ascii scales (for zoomed view)")); }
override public RMUD.MudObject GetObject(string Path) { Path = Path.Replace('\\', '/'); String BasePath, InstanceName; SplitObjectName(Path, out BasePath, out InstanceName); if (!String.IsNullOrEmpty(InstanceName)) { MudObject activeInstance = null; if (ActiveInstances.TryGetValue(Path, out activeInstance)) { return(activeInstance); } else { return(CreateInstance(Path)); } } else { MudObject r = null; if (NamedObjects.ContainsKey(BasePath)) { r = NamedObjects[BasePath]; } else { var typeName = BaseObjectName + "." + Path.Replace("/", "."); var type = SourceAssembly.GetType(typeName); if (type == null) { return(null); } r = Activator.CreateInstance(type) as MudObject; if (r != null) { r.Path = Path; r.State = ObjectState.Unitialized; NamedObjects.Upsert(BasePath, r); } } if (r != null && r.State == ObjectState.Unitialized) { MudObject.InitializeObject(r); } return(r); } }
public static void AddNamedObject(string fullName, object obj) { // 由于创建文件在 windows 下大小写不敏感,所以名字需要大小写不敏感。 string lower = fullName.ToLower(); if (NamedObjects.ContainsKey(lower)) { throw new Exception("duplicate name(Not Case Sensitive): " + fullName); } NamedObjects.Add(lower, obj); }
override public void Initialize() { Core.SettingsObject = new Settings(); var settings = GetObject("settings") as Settings; if (settings == null) { Core.LogError("No settings object found in database. Using default settings."); } else { Core.SettingsObject = settings; } NamedObjects.Clear(); }
public static T GetNamedObject <T>(string fullName) { string lower = fullName.ToLower(); object value = null; if (NamedObjects.TryGetValue(lower, out value)) { if (value is T) { return((T)value); } throw new Exception("NamedObject is not " + fullName); // 怎么得到模板参数类型? } throw new Exception("NamedObject not found(Not Case Sensitive): " + fullName); }
override public MudObject ReloadObject(String Path) { Path = Path.Replace('\\', '/'); if (NamedObjects.ContainsKey(Path)) { var existing = NamedObjects[Path]; var newObject = CompileObject(Path); if (newObject == null) { return(null); } existing.State = ObjectState.Destroyed; NamedObjects.Upsert(Path, newObject); MudObject.InitializeObject(newObject); //Preserve contents if (existing is Container && newObject is Container) { foreach (var item in (existing as Container).EnumerateObjectsAndRelloc()) { (newObject as Container).Add(item.Item1, item.Item2); item.Item1.Location = newObject; } } //Preserve location if (existing is MudObject && newObject is MudObject) { if ((existing as MudObject).Location != null) { var loc = ((existing as MudObject).Location as Container).RelativeLocationOf(existing); MudObject.Move(newObject as MudObject, (existing as MudObject).Location, loc); MudObject.Move(existing as MudObject, null, RelativeLocations.None); } } existing.Destroy(false); return(newObject); } else { return(GetObject(Path)); } }
override public MudObject GetObject(String Path) { Path = Path.Replace('\\', '/'); String BasePath, InstanceName; SplitObjectName(Path, out BasePath, out InstanceName); if (!String.IsNullOrEmpty(InstanceName)) { MudObject activeInstance = null; if (ActiveInstances.TryGetValue(Path, out activeInstance)) { return(activeInstance); } else { return(CreateInstance(Path)); } } else { MudObject r = null; if (NamedObjects.ContainsKey(BasePath)) { r = NamedObjects[BasePath]; } else { r = CompileObject(BasePath); if (r != null) { NamedObjects.Upsert(BasePath, r); } } if (r != null && r.State == ObjectState.Unitialized) { Core.InitializeObject(r); } return(r); } }
override public MudObject ReloadObject(String Path) { Path = Path.Replace('\\', '/'); if (NamedObjects.ContainsKey(Path)) { var existing = NamedObjects[Path]; var newObject = CompileObject(Path); if (newObject == null) { return(null); } existing.State = ObjectState.Destroyed; NamedObjects.Upsert(Path, newObject); Core.InitializeObject(newObject); //Preserve contents foreach (var item in existing.EnumerateObjectsAndRelloc()) { newObject.Add(item.Item1, item.Item2); item.Item1.Location = newObject; } //Preserve location if (existing is MudObject _existing && newObject is MudObject _newObject) { if (_existing.Location.HasValue(out var loc) && _newObject.Location.HasValue(out var newLoc)) { var relloc = loc.RelativeLocationOf(existing); Core.Move(newObject as MudObject, newLoc, relloc); Core.Move(existing as MudObject, null, RelativeLocations.NONE); } } existing.Destroy(false); return(newObject); } else { return(GetObject(Path)); } }
private void InitialBulkCompile(Action <String> ReportErrors) { if (NamedObjects.Count != 0) //That is, if anything besides Settings has been loaded... { throw new InvalidOperationException("Bulk compilation must happen before any other objects are loaded or bad things happen."); } var fileTable = new List <FileTableEntry>(); var source = new StringBuilder(); source.Append(GetFileHeader() + "namespace database {\n"); int lineCount = 5; var fileList = EnumerateLocalDatabase(""); foreach (var s in fileList) { Console.WriteLine(s); fileTable.Add(new FileTableEntry { Path = s, FirstLine = lineCount }); lineCount += 4; source.AppendFormat("namespace {0} {{\n", PathToNamespace(s)); var fileSource = PreprocessSourceFile(s); lineCount += fileSource.Count(c => c == '\n'); source.Append(fileSource); source.Append("\n}\n\n"); } source.Append("}\n"); var combinedAssembly = CompileCode(source.ToString(), "/*", i => { var r = fileTable.Reverse <FileTableEntry>().FirstOrDefault(e => e.FirstLine < i); if (r != null) { return(r.Path); } return(""); }); if (combinedAssembly != null) { foreach (var s in fileList) { var qualifiedName = String.Format("database.{0}.{1}", PathToNamespace(s), System.IO.Path.GetFileNameWithoutExtension(s)); var newObjectType = combinedAssembly.GetType(qualifiedName); if (newObjectType == null) { ReportErrors(String.Format("Type {0} not found in combined assembly.", qualifiedName)); } else { var newObject = Activator.CreateInstance(combinedAssembly.GetType(qualifiedName)); if (!(newObject is MudObject)) { ReportErrors(String.Format("Type {0} was found, but was not a mud object.", qualifiedName)); } else { var mudObject = newObject as MudObject; mudObject.Path = s; mudObject.State = ObjectState.Unitialized; foreach (var method in newObject.GetType().GetMethods()) { if (method.IsStatic && method.Name == "AtStartup") { method.Invoke(null, new Object[] { Core.GlobalRules }); } } NamedObjects.Upsert(s, mudObject); } } } } }