From 8d7e38039b266e7e8123ae3ab2ba57ebf6c12a77 Mon Sep 17 00:00:00 2001 From: Major Cooke Date: Sat, 19 Dec 2020 10:58:27 -0600 Subject: [PATCH] Merged https://github.com/coelckers/gzdoom/pull/1087 (service interface) --- wadsrc/static/zscript.txt | 1 + wadsrc/static/zscript/engine/service.zs | 99 +++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 wadsrc/static/zscript/engine/service.zs diff --git a/wadsrc/static/zscript.txt b/wadsrc/static/zscript.txt index b6257f1c9..52b2441ff 100644 --- a/wadsrc/static/zscript.txt +++ b/wadsrc/static/zscript.txt @@ -5,6 +5,7 @@ version "4.5" #include "zscript/engine/dynarrays.zs" #include "zscript/engine/dictionary.zs" #include "zscript/engine/inputevents.zs" +#include "zscript/engine/service.zs" #include "zscript/engine/ui/menu/colorpickermenu.zs" #include "zscript/engine/ui/menu/joystickmenu.zs" diff --git a/wadsrc/static/zscript/engine/service.zs b/wadsrc/static/zscript/engine/service.zs new file mode 100644 index 000000000..16a731f1f --- /dev/null +++ b/wadsrc/static/zscript/engine/service.zs @@ -0,0 +1,99 @@ +/** + * This is Service interface. + */ +class Service abstract +{ + virtual play String Get(String request) + { + return ""; + } + + virtual ui String UiGet(String request) + { + return ""; + } +} + +/** + * Use this class to find and iterate over services. + * + * Example usage: + * + * @code + * ServiceIterator i = ServiceIterator.Find("MyService"); + * + * Service s; + * while (s = i.Next()) + * { + * String request = ... + * String answer = s.Get(request); + * ... + * } + * @endcode + * + * If no services are found, the all calls to Next() will return NULL. + */ +class ServiceIterator +{ + /** + * Creates a Service iterator for a service name. It will iterate over all existing Services + * with names that match @a serviceName or have it as a part of their names. + * + * Matching is case-independent. + * + * @param serviceName class name of service to find. + */ + static ServiceIterator Find(String serviceName) + { + let result = new("ServiceIterator"); + + result.mServiceName = serviceName; + result.mClassIndex = 0; + result.FindNextService(); + + return result; + } + + /** + * Gets the service and advances the iterator. + * + * @returns service instance, or NULL if no more servers found. + * + * @note Each ServiceIterator will return new instances of services. + */ + Service Next() + { + uint classesNumber = AllClasses.Size(); + Service result = (mClassIndex == classesNumber) + ? NULL + : Service(new(AllClasses[mClassIndex])); + + ++mClassIndex; + FindNextService(); + + return result; + } + + private void FindNextService() + { + uint classesNumber = AllClasses.size(); + while (mClassIndex < classesNumber && !ServiceNameContains(AllClasses[mClassIndex], mServiceName)) + { + ++mClassIndex; + } + } + + private static bool ServiceNameContains(class aClass, String substring) + { + if (!(aClass is "Service")) return false; + + String className = aClass.GetClassName(); + String lowerClassName = className.MakeLower(); + String lowerSubstring = substring.MakeLower(); + bool result = lowerClassName.IndexOf(lowerSubstring) != -1; + return result; + } + + private String mServiceName; + private uint mClassIndex; +}