Skip to content

Commit

Permalink
Improve Services
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 authored and coelckers committed Feb 19, 2023
1 parent b0dfa06 commit 590475a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 36 deletions.
7 changes: 7 additions & 0 deletions src/common/scripting/interface/vmnatives.cpp
Expand Up @@ -53,6 +53,8 @@
#include "s_soundinternal.h"
#include "i_time.h"

#include "maps.h"

//==========================================================================
//
// status bar exports
Expand Down Expand Up @@ -1159,7 +1161,12 @@ DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, baseorder);
DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, loop);
DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, handle);

extern ZSMap<FName, DObject* > AllServices;

DEFINE_GLOBAL_NAMED(PClass::AllClasses, AllClasses)

DEFINE_GLOBAL(AllServices)

DEFINE_GLOBAL(Bindings)
DEFINE_GLOBAL(AutomapBindings)
DEFINE_GLOBAL(generic_ui)
Expand Down
27 changes: 27 additions & 0 deletions src/gamedata/info.cpp
Expand Up @@ -54,6 +54,7 @@
#include "g_levellocals.h"
#include "texturemanager.h"
#include "d_main.h"
#include "maps.h"

extern void LoadActors ();
extern void InitBotStuff();
Expand All @@ -64,6 +65,7 @@ FRandom FState::pr_statetics("StateTics");

cycle_t ActionCycles;

void InitServices();

//==========================================================================
//
Expand Down Expand Up @@ -368,6 +370,18 @@ static void LoadAltHudStuff()
//
//==========================================================================

ZSMap<FName, DObject*> AllServices;

static void MarkServices(){

ZSMap<FName, DObject*>::Iterator it(AllServices);
ZSMap<FName, DObject*>::Pair *pair;
while (it.NextPair(pair))
{
GC::Mark<DObject>(pair->Value);
}
}

void PClassActor::StaticInit()
{
sprites.Clear();
Expand Down Expand Up @@ -402,6 +416,19 @@ void PClassActor::StaticInit()
}
}

PClass * cls = PClass::FindClass("Service");
for(PClass * clss : PClass::AllClasses)
{
if(clss != cls && cls->IsAncestorOf(clss))
{
DObject * obj = clss->CreateNew();
obj->ObjectFlags |= OF_Transient;
AllServices.Insert(clss->TypeName, obj);
}
}

GC::AddMarkerFunc(&MarkServices);

LoadAltHudStuff();
InitBotStuff();

Expand Down
1 change: 1 addition & 0 deletions wadsrc/static/zscript/engine/base.zs
Expand Up @@ -181,6 +181,7 @@ struct Vector3
struct _ native // These are the global variables, the struct is only here to avoid extending the parser for this.
{
native readonly Array<class> AllClasses;
native internal readonly Map<Name , Service> AllServices;
native readonly bool multiplayer;
native @KeyBindings Bindings;
native @KeyBindings AutomapBindings;
Expand Down
49 changes: 13 additions & 36 deletions wadsrc/static/zscript/engine/service.zs
Expand Up @@ -54,6 +54,10 @@ class Service abstract
{
return null;
}

static Service Find(class<Service> serviceName){
return AllServices.GetIfExists(serviceName.GetClassName());
}
}

/**
Expand Down Expand Up @@ -88,54 +92,27 @@ class ServiceIterator
static ServiceIterator Find(String serviceName)
{
let result = new("ServiceIterator");

result.mServiceName = serviceName;
result.mClassIndex = 0;
result.FindNextService();

result.mServiceName = serviceName.MakeLower();
result.it.Init(AllServices);
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.
* @returns service instance, or NULL if no more services found.
*/
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))
while(it.Next())
{
++mClassIndex;
String cName = it.GetKey();
if(cName.MakeLower().IndexOf(mServiceName) != -1);
return it.GetValue();
}
return null;
}

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 MapIterator<Name, Service> it;
private String mServiceName;
private uint mClassIndex;
}

0 comments on commit 590475a

Please sign in to comment.