Skip to content

Commit

Permalink
More CLEO5 things + CLEOAddon funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
RusJJ committed Apr 5, 2024
1 parent 9c3425d commit a208af6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 17 deletions.
27 changes: 15 additions & 12 deletions cleo4opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ CLEO_Fn(OPEN_FILE)
CLEO_ReadStringEx(handle, filename, sizeof(filename));
CLEO_ReadStringEx(handle, mode, sizeof(mode));

FILE* file = DoFile(filename, mode);
int i = 0; while(filename[i] != 0) // A little hack (cheeseburger is like WHAAAA)
{
if(filename[i] == '\\') filename[i] = '/';
++i;
}
std::string str = ResolvePath(handle, filename);
FILE* file = DoFile(str.c_str(), mode);
cleo->GetPointerToScriptVar(handle)->i = (int)file;
UpdateCompareFlag(handle, file != NULL);
}
Expand Down Expand Up @@ -1131,10 +1137,9 @@ CLEO_Fn(DOES_DIRECTORY_EXIST)
++i;
}

char path[256];
snprintf(path, sizeof(path), "%s/%s", aml->GetAndroidDataPath(), filepath);
std::string str = ResolvePath(handle, filepath);

DIR* dir = opendir(path);
DIR* dir = opendir(str.c_str());
UpdateCompareFlag(handle, dir != NULL);
if(dir) closedir(dir);
}
Expand All @@ -1149,10 +1154,9 @@ CLEO_Fn(CREATE_DIRECTORY)
++i;
}

char path[256];
snprintf(path, sizeof(path), "%s/%s", aml->GetAndroidDataPath(), filepath);
std::string str = ResolvePath(handle, filepath);

int result = mkdir(path, 0777);
int result = mkdir(str.c_str(), 0777);
UpdateCompareFlag(handle, result == 0);
}

Expand All @@ -1166,24 +1170,23 @@ CLEO_Fn(FIND_FIRST_FILE)
++i;
}

char path[256];
snprintf(path, sizeof(path), "%s/%s", aml->GetAndroidDataPath(), filepath);
std::string str = ResolvePath(handle, filepath);

DIR* dir = opendir(path);
DIR* dir = opendir(str.c_str());
if(dir)
{
struct dirent *entry;
struct stat buf;
char filecheckpath[256];
while((entry = readdir(dir)) != NULL)
{
snprintf(filecheckpath, sizeof(filecheckpath), "%s/%s", path, entry->d_name);
snprintf(filecheckpath, sizeof(filecheckpath), "%s/%s", str.c_str(), entry->d_name);
lstat(filecheckpath, &buf);

if(!S_ISDIR(buf.st_mode))
{
CLEO_DirScan *scan = new CLEO_DirScan;
strncpy(scan->path, path, sizeof(scan->path));
strncpy(scan->path, str.c_str(), sizeof(scan->path));
scan->dir = dir;

CLEO_WriteStringEx(handle, entry->d_name);
Expand Down
78 changes: 78 additions & 0 deletions cleo5opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,78 @@ CLEO_Fn(GET_SCRIPT_FILENAME)
}
}
}
CLEO_Fn(DELETE_FILE)
{
char path[MAX_STR_LEN];
CLEO_ReadStringEx(handle, path, sizeof(path));
std::string str = ResolvePath(handle, path);
int removeStatus = remove(str.c_str());
UpdateCompareFlag(handle, removeStatus == 0);
}
CLEO_Fn(DELETE_DIRECTORY)
{
char path[MAX_STR_LEN];
CLEO_ReadStringEx(handle, path, sizeof(path));
std::string str = ResolvePath(handle, path);
bool deleteContents = cleo->ReadParam(handle)->i;
bool removed = deleteContents ? fs::remove(str.c_str()) : fs::remove_all(str.c_str());
UpdateCompareFlag(handle, removed);
}
CLEO_Fn(MOVE_FILE)
{
char from[MAX_STR_LEN];
CLEO_ReadStringEx(handle, from, sizeof(from));
std::string fromstr = ResolvePath(handle, from);

char to[MAX_STR_LEN];
CLEO_ReadStringEx(handle, to, sizeof(to));
std::string tostr = ResolvePath(handle, to);

std::error_code ec; ec.clear();
fs::rename(from, to, ec);
UpdateCompareFlag(handle, ec.value() == 0);
}
CLEO_Fn(MOVE_DIRECTORY)
{
char from[MAX_STR_LEN];
CLEO_ReadStringEx(handle, from, sizeof(from));
std::string fromstr = ResolvePath(handle, from);

char to[MAX_STR_LEN];
CLEO_ReadStringEx(handle, to, sizeof(to));
std::string tostr = ResolvePath(handle, to);

std::error_code ec; ec.clear();
fs::rename(from, to, ec);
UpdateCompareFlag(handle, ec.value() == 0);
}
CLEO_Fn(COPY_FILE)
{
char from[MAX_STR_LEN];
CLEO_ReadStringEx(handle, from, sizeof(from));
std::string fromstr = ResolvePath(handle, from);

char to[MAX_STR_LEN];
CLEO_ReadStringEx(handle, to, sizeof(to));
std::string tostr = ResolvePath(handle, to);

bool copied = fs::copy_file(from, to, fs::copy_options::skip_existing);
UpdateCompareFlag(handle, copied);
}
CLEO_Fn(COPY_DIRECTORY)
{
char from[MAX_STR_LEN];
CLEO_ReadStringEx(handle, from, sizeof(from));
std::string fromstr = ResolvePath(handle, from);

char to[MAX_STR_LEN];
CLEO_ReadStringEx(handle, to, sizeof(to));
std::string tostr = ResolvePath(handle, to);

std::error_code ec; ec.clear();
fs::copy(from, to, fs::copy_options::skip_existing | fs::copy_options::directories_only, ec);
UpdateCompareFlag(handle, ec.value() == 0);
}

// CLEO 5
CLEO_Fn(CLEO_RETURN_WITH)
Expand Down Expand Up @@ -390,6 +462,12 @@ void Init5Opcodes()
CLEO_RegisterOpcode(0x2302, WRITE_BLOCK_TO_FILE); // 2302=3, write_block_to_file %1d% size %2d% address %3d% // IF and SET
CLEO_RegisterOpcode(0x2303, RESOLVE_FILEPATH); // 2303=2, %2s% = resolve_filepath %1s%
CLEO_RegisterOpcode(0x2304, GET_SCRIPT_FILENAME); // 2304=3, %3s% = get_script_filename %1d% full_path %2d% // IF and SET
CLEO_RegisterOpcode(0x0B00, DELETE_FILE); // 0B00=1, delete_file %1s% //IF and SET
CLEO_RegisterOpcode(0x0B01, DELETE_DIRECTORY); // 0B01=1, delete_directory %1s% with_all_files_and_subdirectories %2d% //IF and SET
CLEO_RegisterOpcode(0x0B02, MOVE_FILE); // 0B02=2, move_file %1s% to %2s% //IF and SET
CLEO_RegisterOpcode(0x0B03, MOVE_DIRECTORY); // 0B03=2, move_directory %1s% to %2s% //IF and SET
CLEO_RegisterOpcode(0x0B04, COPY_FILE); // 0B04=2, copy_file %1s% to %2s% //IF and SET
CLEO_RegisterOpcode(0x0B05, COPY_DIRECTORY); // 0B05=2, copy_directory %1d% to %2d% //IF and SET

// CLEO 5
CLEO_RegisterOpcode(0x2002, CLEO_RETURN_WITH); // 2002=-1, cleo_return_with ...
Expand Down
3 changes: 3 additions & 0 deletions cleoaddon.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _CLEO_ADDON_H

#include <stdint.h>
#include <string.h>

#define CLEO_RegisterOpcode(x, h) cleo->RegisterOpcode(x, h); cleo->RegisterOpcodeFunction(#h, h)
#define CLEO_Fn(h) void h (void *handle, uint32_t *ip, uint16_t opcode, const char *name)
Expand Down Expand Up @@ -65,6 +66,8 @@ struct cleo_addon_ifs_t
void (*UpdateCompareFlag)(void* handle, uint8_t flag);
bool (*IsOpcodeAlreadyExists)(uint16_t opcode);
bool (*IsValidScriptHandle)(void* handle);
std::string (*ResolvePath)(void* handle, const char* path, const char* customWorkDir);
void (*AddGXTLabel)(const char* gxtLabel, const char* text);
};

#endif // _CLEO_ADDON_H
12 changes: 7 additions & 5 deletions cleohelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extern uint8_t* ScriptSpace;
extern int* pScriptsStorage, *pScriptsStorageEnd;
extern void (*UpdateCompareFlag)(void*, uint8_t);

namespace fs = std::filesystem;

#define CLEO_RegisterOpcode(x, h) cleo->RegisterOpcode(x, h); cleo->RegisterOpcodeFunction(#h, h)
#define CLEO_Fn(h) void h (void *handle, uint32_t *ip, uint16_t opcode, const char *name)

Expand Down Expand Up @@ -968,8 +970,8 @@ inline std::string ResolvePath(void* handle, const char* path, const char* custo
if(!path) return "";

enum class VPref{ None, Game, User, Script, Cleo, Modules } virtualPrefix = VPref::None;
std::filesystem::path fsPath = std::filesystem::path(path);
std::filesystem::path::iterator root = fsPath.begin();
fs::path fsPath = fs::path(path);
fs::path::iterator root = fsPath.begin();
if(root != fsPath.end())
{
if(*root == DIR_GAME) virtualPrefix = VPref::Game;
Expand All @@ -979,7 +981,7 @@ inline std::string ResolvePath(void* handle, const char* path, const char* custo
else if (*root == DIR_MODULES) virtualPrefix = VPref::Modules;
}

std::filesystem::path resolved;
fs::path resolved;
switch(virtualPrefix)
{
default: //case VPref::None:
Expand All @@ -991,7 +993,7 @@ inline std::string ResolvePath(void* handle, const char* path, const char* custo
else
fsPath = GetScriptWorkDir(handle) / fsPath;
}
return std::filesystem::weakly_canonical(fsPath).string();
return fs::weakly_canonical(fsPath).string();
}

case VPref::User:
Expand All @@ -1018,5 +1020,5 @@ inline std::string ResolvePath(void* handle, const char* path, const char* custo
}

for(auto it = ++fsPath.begin(); it != fsPath.end(); it++) resolved /= *it;
return std::filesystem::weakly_canonical(resolved).string(); // collapse "..\" uses
return fs::weakly_canonical(resolved).string(); // collapse "..\" uses
}
3 changes: 3 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ DECL_HOOK(int8_t, ProcessOneCommand, void* handle)
return retCode;
}

void AddGXTLabel(const char* gxtLabel, const char* text);
extern "C" void OnModPreLoad()
{
logger->SetTag("CLEO Mod");
Expand Down Expand Up @@ -396,6 +397,8 @@ extern "C" void OnModPreLoad()
return (fn != NULL && *fn != NULL);
};
cleo_addon_ifs.IsValidScriptHandle = IsValidScriptHandle;
cleo_addon_ifs.ResolvePath = ResolvePath;
cleo_addon_ifs.AddGXTLabel = AddGXTLabel;
RegisterInterface("CLEOAddon", &cleo_addon_ifs);
logger->Info("CLEO Addon Initialized!");
}
Expand Down

0 comments on commit a208af6

Please sign in to comment.