Skip to content

Commit

Permalink
- added the flag parsing utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Jan 23, 2022
1 parent e035ce0 commit 531a42e
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 5 deletions.
1 change: 1 addition & 0 deletions source/CMakeLists.txt
Expand Up @@ -1015,6 +1015,7 @@ set (PCH_SOURCES
core/actorinfo.cpp
core/zcc_compile_raze.cpp
core/thingdef_data.cpp
core/thingdef_properties.cpp
core/actorlist.cpp
core/automap.cpp
core/cheats.cpp
Expand Down
6 changes: 6 additions & 0 deletions source/core/coreactor.h
Expand Up @@ -444,3 +444,9 @@ inline PClassActor* PClass::FindActor(FName name)
auto cls = FindClass(name);
return cls && cls->IsDescendantOf(RUNTIME_CLASS(DCoreActor)) ? static_cast<PClassActor*>(cls) : nullptr;
}

inline DCoreActor* GetDefaultByType(const PClass* type)
{
return (DCoreActor*)(type->Defaults);
}

4 changes: 1 addition & 3 deletions source/core/thingdef.h
Expand Up @@ -67,17 +67,15 @@ struct FFlagDef
int varflags;
};

#if 0
void FinalizeClass(PClass *cls);
FFlagDef *FindFlag (const PClass *type, const char *part1, const char *part2, bool strict = false);
const char* GetFlagName(unsigned int flagnum, int flagoffset);
void HandleDeprecatedFlags(DCoreActor *defaults, PClassActor *info, bool set, int index);
bool CheckDeprecatedFlags(DCoreActor *actor, PClassActor *info, int index);
const char *GetFlagName(unsigned int flagnum, int flagoffset);
void ModActorFlag(DCoreActor *actor, FFlagDef *fd, bool set);
bool ModActorFlag(DCoreActor *actor, const FString &flagname, bool set, bool printerror = true);
INTBOOL CheckActorFlag(DCoreActor *actor, FFlagDef *fd);
INTBOOL CheckActorFlag(DCoreActor *owner, const char *flagname, bool printerror = true);
#endif

#define FLAG_NAME(flagnum, flagvar) GetFlagName(flagnum, myoffsetof(DCoreActor, flagvar))

Expand Down
305 changes: 305 additions & 0 deletions source/core/thingdef_properties.cpp
@@ -0,0 +1,305 @@
/*
** thingdef-properties.cpp
**
** Actor denitions - properties and flags handling
**
**---------------------------------------------------------------------------
** Copyright 2002-2007 Christoph Oelckers
** Copyright 2004-2007 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
** 4. When not used as part of ZDoom or a ZDoom derivative, this code will be
** covered by the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or (at
** your option) any later version.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/

#include "gi.h"
#include "filesystem.h"
#include "cmdlib.h"
#include "v_text.h"
#include "thingdef.h"
#include "vmbuilder.h"
#include "types.h"
#include "v_video.h"
#include "texturemanager.h"
#include "coreactor.h"
#include "thingdef.h"

//==========================================================================
//
// Gets a class pointer and performs an error check for correct type
//
//==========================================================================
static PClassActor *FindClassTentative(const char *name, PClass *ancestor, bool optional = false)
{
// "" and "none" mean 'no class'
if (name == NULL || *name == 0 || !stricmp(name, "none"))
{
return NULL;
}

PClass *cls = ancestor->FindClassTentative(name);
assert(cls != NULL); // cls can not be NULL here
if (!cls->IsDescendantOf(ancestor))
{
I_Error("%s does not inherit from %s\n", name, ancestor->TypeName.GetChars());
}
if (cls->Size == TentativeClass && optional)
{
cls->bOptional = true;
}
return static_cast<PClassActor *>(cls);
}

//==========================================================================
//
// Sets or clears a flag, taking field width into account.
//
//==========================================================================
void ModActorFlag(DCoreActor *actor, FFlagDef *fd, bool set)
{
// Little-Endian machines only need one case, because all field sizes
// start at the same address. (Unless the machine has unaligned access
// exceptions, in which case you'll need multiple cases for it too.)
#ifdef __BIG_ENDIAN__
if (fd->fieldsize == 4)
#endif
{
uint32_t *flagvar = (uint32_t *)((char *)actor + fd->structoffset);
if (set)
{
*flagvar |= fd->flagbit;
}
else
{
*flagvar &= ~fd->flagbit;
}
}
#ifdef __BIG_ENDIAN__
else if (fd->fieldsize == 2)
{
uint16_t *flagvar = (uint16_t *)((char *)actor + fd->structoffset);
if (set)
{
*flagvar |= fd->flagbit;
}
else
{
*flagvar &= ~fd->flagbit;
}
}
else
{
assert(fd->fieldsize == 1);
uint8_t *flagvar = (uint8_t *)((char *)actor + fd->structoffset);
if (set)
{
*flagvar |= fd->flagbit;
}
else
{
*flagvar &= ~fd->flagbit;
}
}
#endif
}

//==========================================================================
//
// Finds a flag by name and sets or clears it
//
// Returns true if the flag was found for the actor; else returns false
//
//==========================================================================

bool ModActorFlag(DCoreActor *actor, const FString &flagname, bool set, bool printerror)
{
bool found = false;

if (actor != NULL)
{
const char *dot = strchr(flagname, '.');
FFlagDef *fd;
PClassActor* cls = static_cast<PClassActor*>(actor->GetClass());

if (dot != NULL)
{
FString part1(flagname.GetChars(), dot - flagname);
fd = FindFlag(cls, part1, dot + 1);
}
else
{
fd = FindFlag(cls, flagname, NULL);
}

if (fd != NULL)
{
found = true;

if (fd->structoffset == -1)
{
HandleDeprecatedFlags(actor, cls, set, fd->flagbit);
}
}
else if (printerror)
{
DPrintf(DMSG_ERROR, "ACS/DECORATE: '%s' is not a flag in '%s'\n", flagname.GetChars(), cls->TypeName.GetChars());
}
}

return found;
}

//==========================================================================
//
// Returns whether an actor flag is true or not.
//
//==========================================================================

INTBOOL CheckActorFlag(DCoreActor *owner, FFlagDef *fd)
{
if (fd->structoffset == -1)
{
return CheckDeprecatedFlags(owner, static_cast<PClassActor*>(owner->GetClass()), fd->flagbit);
}
else
#ifdef __BIG_ENDIAN__
if (fd->fieldsize == 4)
#endif
{
return fd->flagbit & *(uint32_t *)(((char*)owner) + fd->structoffset);
}
#ifdef __BIG_ENDIAN__
else if (fd->fieldsize == 2)
{
return fd->flagbit & *(uint16_t *)(((char*)owner) + fd->structoffset);
}
else
{
assert(fd->fieldsize == 1);
return fd->flagbit & *(uint8_t *)(((char*)owner) + fd->structoffset);
}
#endif
}

INTBOOL CheckActorFlag(DCoreActor *owner, const char *flagname, bool printerror)
{
const char *dot = strchr (flagname, '.');
FFlagDef *fd;
const PClass *cls = owner->GetClass();

if (dot != NULL)
{
FString part1(flagname, dot-flagname);
fd = FindFlag (cls, part1, dot+1);
}
else
{
fd = FindFlag (cls, flagname, NULL);
}

if (fd != NULL)
{
return CheckActorFlag(owner, fd);
}
else
{
if (printerror) Printf("Unknown flag '%s' in '%s'\n", flagname, cls->TypeName.GetChars());
return false;
}
}

//===========================================================================
//
// HandleDeprecatedFlags
//
// Handles the deprecated flags and sets the respective properties
// to appropriate values. This is solely intended for backwards
// compatibility so mixing this with code that is aware of the real
// properties is not recommended
//
//===========================================================================
void HandleDeprecatedFlags(DCoreActor *defaults, PClassActor *info, bool set, int index)
{
}

//===========================================================================
//
// CheckDeprecatedFlags
//
// Checks properties related to deprecated flags, and returns true only
// if the relevant properties are configured exactly as they would have
// been by setting the flag in HandleDeprecatedFlags.
//
//===========================================================================

bool CheckDeprecatedFlags(DCoreActor *actor, PClassActor *info, int index)
{
return false; // Any entirely unknown flag is not set
}

//==========================================================================
//
//
//
//==========================================================================
int MatchString (const char *in, const char **strings)
{
int i;

for (i = 0; *strings != NULL; i++)
{
if (!stricmp(in, *strings++))
{
return i;
}
}
return -1;
}

//==========================================================================
//
// Get access to scripted pointers.
// They need a bit more work than other variables.
//
//==========================================================================

static bool PointerCheck(PType *symtype, PType *checktype)
{
auto symptype = PType::toClassPointer(symtype);
auto checkptype = PType::toClassPointer(checktype);
return symptype != nullptr && checkptype != nullptr && symptype->ClassRestriction->IsDescendantOf(checkptype->ClassRestriction);
}

//==========================================================================
//
// Info Property handlers
//
//==========================================================================

// todo
2 changes: 0 additions & 2 deletions source/core/zcc_compile_raze.cpp
Expand Up @@ -602,7 +602,6 @@ void ZCCRazeCompiler::ProcessDefaultFlag(PClassActor *cls, ZCC_FlagStmt *flg)
return;
}

#if 0
auto fd = FindFlag(cls, n1, n2, true);
if (fd != nullptr)
{
Expand All @@ -620,7 +619,6 @@ void ZCCRazeCompiler::ProcessDefaultFlag(PClassActor *cls, ZCC_FlagStmt *flg)
}
}
else
#endif
{
Error(flg, "Unknown flag '%s%s%s'", n1, n2 ? "." : "", n2 ? n2 : "");
}
Expand Down

0 comments on commit 531a42e

Please sign in to comment.