Tool refactoring
This commit is contained in:
1 parent
f267131d6b
commit
9e2f093527
41 files changed
+428
-221
No files matched your search
+2
-3
@@ -82,6 +82,5 @@ assets/borrowed
|
|||||||
.vscode*
|
.vscode*
|
||||||
.VSCode*
|
.VSCode*
|
||||||
|
|
||||||
/vita/*
|
/vita
|
||||||
/tools/*
|
/tools
|
||||||
./tools/*
|
|
||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.13)
|
cmake_minimum_required(VERSION 3.13)
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
|
||||||
|
|
||||||
|
|||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,17 @@
|
|||||||
|
# Copyright (c) 2023 Dominic Msters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
set(D ${CMAKE_CURRENT_LIST_DIR})
|
||||||
|
|
||||||
|
set(
|
||||||
|
DAWN_TOOL_SOURCES
|
||||||
|
${D}/DawnTool.cpp
|
||||||
|
${D}/File.cpp
|
||||||
|
${D}/Language.cpp
|
||||||
|
${D}/CodeGen.cpp
|
||||||
|
|
||||||
|
CACHE INTERNAL
|
||||||
|
${DAWN_CACHE_TARGET}
|
||||||
|
)
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2023 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dawnsharedlibs.hpp"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all instances of a character from a C-Styled string.
|
||||||
|
*
|
||||||
|
* @param string String to remove characters from.
|
||||||
|
* @param remove Character to remove.
|
||||||
|
*/
|
||||||
|
static inline void stringRemoveAll(char *string, char remove) {
|
||||||
|
size_t len = strlen(string);
|
||||||
|
size_t i, j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(i < len) {
|
||||||
|
char c = string[i];
|
||||||
|
if(c != remove) {
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
j = i + 1;
|
||||||
|
while(j < len) {
|
||||||
|
string[j-1] = string[j];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
@@ -5,11 +5,7 @@
|
|||||||
|
|
||||||
# Check for build target, or default. This is pretty much not guaranteed.
|
# Check for build target, or default. This is pretty much not guaranteed.
|
||||||
if(NOT DEFINED DAWN_BUILD_TARGET)
|
if(NOT DEFINED DAWN_BUILD_TARGET)
|
||||||
if(WIN32)
|
set(DAWN_BUILD_TARGET "target-tools")
|
||||||
set(DAWN_BUILD_TARGET "target-rose-win32-glfw")
|
|
||||||
elseif(UNIX AND NOT APPLE)
|
|
||||||
set(DAWN_BUILD_TARGET "target-rose-linux64-glfw")
|
|
||||||
endif()
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Now validate we have a build target for real
|
# Now validate we have a build target for real
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# Copyright (c) 2023 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
set(DAWN_BUILDING dawnrose CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
|
set(DAWN_TARGET_WIN32 true CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
|
set(DAWN_TARGET_GLFW true CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
|
set(DAWN_TARGET_NAME "Rose" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
@@ -18,12 +18,12 @@ void SphereMesh::createSphere(
|
|||||||
// Create vertices
|
// Create vertices
|
||||||
int32_t c = 0;
|
int32_t c = 0;
|
||||||
for (int32_t i = 0; i <= stacks; i++) {
|
for (int32_t i = 0; i <= stacks; i++) {
|
||||||
float_t phi = M_PI * i / stacks;
|
float_t phi = MATH_PI * i / stacks;
|
||||||
float_t cosPhi = cos(phi);
|
float_t cosPhi = cos(phi);
|
||||||
float_t sinPhi = sin(phi);
|
float_t sinPhi = sin(phi);
|
||||||
|
|
||||||
for (int32_t j = 0; j <= slices; j++) {
|
for (int32_t j = 0; j <= slices; j++) {
|
||||||
float_t theta = 2 * M_PI * j / slices;
|
float_t theta = 2 * MATH_PI * j / slices;
|
||||||
float_t cosTheta = cos(theta);
|
float_t cosTheta = cos(theta);
|
||||||
float_t sinTheta = sin(theta);
|
float_t sinTheta = sin(theta);
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "display/mesh/Mesh.hpp"
|
#include "display/mesh/Mesh.hpp"
|
||||||
|
#include "util/mathutils.hpp"
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class SphereMesh {
|
class SphereMesh {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ bool_t CapsuleCollider::performRaycast(
|
|||||||
|
|
||||||
return raytestCapsule(
|
return raytestCapsule(
|
||||||
ray,
|
ray,
|
||||||
{
|
(struct PhysicsCapsule){
|
||||||
.height = this->height,
|
.height = this->height,
|
||||||
.radius = this->radius,
|
.radius = this->radius,
|
||||||
.origin = this->transform->getWorldPosition()
|
.origin = this->transform->getWorldPosition()
|
||||||
|
|||||||
File renamed without changes.
@@ -30,7 +30,7 @@ void GameCamera::onStart() {
|
|||||||
if(current != slowTarget) {
|
if(current != slowTarget) {
|
||||||
float_t m = 6.0f;
|
float_t m = 6.0f;
|
||||||
float_t s = delta * 3.0f;
|
float_t s = delta * 3.0f;
|
||||||
current += glm::vec2(.
|
current += glm::vec2(
|
||||||
mathClamp<float_t>((slowTarget.x - current.x) * s, -m, m),
|
mathClamp<float_t>((slowTarget.x - current.x) * s, -m, m),
|
||||||
mathClamp<float_t>((slowTarget.y - current.y) * s, -m, m)
|
mathClamp<float_t>((slowTarget.y - current.y) * s, -m, m)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -19,4 +19,4 @@ set(
|
|||||||
include(util/CMakeLists.txt)
|
include(util/CMakeLists.txt)
|
||||||
|
|
||||||
# Tools
|
# Tools
|
||||||
add_subdirectory(tools)
|
add_subdirectory(prefabtool)
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
# Copyright (c) 2023 Dominic Msters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Texture Build Tool
|
||||||
|
project(prefabtool VERSION 1.0)
|
||||||
|
add_executable(prefabtool)
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(prefabtool
|
||||||
|
PRIVATE
|
||||||
|
${DAWN_SHARED_SOURCES}
|
||||||
|
${DAWN_TOOL_SOURCES}
|
||||||
|
PrefabTool.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
# Includes
|
||||||
|
target_include_directories(prefabtool
|
||||||
|
PUBLIC
|
||||||
|
${DAWN_SHARED_INCLUDES}
|
||||||
|
${DAWN_TOOL_INCLUDES}
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Definitions
|
||||||
|
target_compile_definitions(prefabtool
|
||||||
|
PUBLIC
|
||||||
|
DAWN_TOOL_INSTANCE=PrefabTool
|
||||||
|
DAWN_TOOL_HEADER="PrefabTool.hpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Libraries
|
||||||
|
target_link_libraries(prefabtool
|
||||||
|
PUBLIC
|
||||||
|
${DAWN_BUILD_HOST_LIBS}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Tool Function
|
||||||
|
function(tool_prefab target in)
|
||||||
|
add_custom_target(${target}
|
||||||
|
COMMAND prefabtool --input="${DAWN_ASSETS_SOURCE_DIR}/${in}"
|
||||||
|
COMMENT "Generating prefab ${target} from ${in}"
|
||||||
|
DEPENDS prefabtool
|
||||||
|
)
|
||||||
|
add_dependencies(${DAWN_TARGET_NAME} ${target})
|
||||||
|
endfunction()
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright (c) 2023 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#include "PrefabTool.hpp"
|
||||||
|
|
||||||
|
using namespace Dawn;
|
||||||
|
|
||||||
|
std::vector<std::string> PrefabTool::getRequiredFlags() {
|
||||||
|
return { "input" };
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string, std::string> PrefabTool::getOptionalFlags() {
|
||||||
|
return std::map<std::string, std::string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t PrefabTool::start() {
|
||||||
|
File input = File(flags["input"]);
|
||||||
|
if(!input.exists()) {
|
||||||
|
std::cout << "Input file does not exist!" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Input: " << input.filename << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// Copyright (c) 2023 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "util/DawnTool.hpp"
|
||||||
|
#include "util/File.hpp"
|
||||||
|
|
||||||
|
namespace Dawn {
|
||||||
|
class PrefabTool : public DawnTool {
|
||||||
|
protected:
|
||||||
|
std::vector<std::string> getRequiredFlags() override;
|
||||||
|
std::map<std::string, std::string> getOptionalFlags() override;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int32_t start();
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -5,9 +5,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "dawnsharedlibs.hpp"
|
#include "dawnsharedlibs.hpp"
|
||||||
#include "../../util/file.hpp"
|
|
||||||
#include "../../util/csv.hpp"
|
|
||||||
#include "../../util/xml.hpp"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|||||||
+146
-152
@@ -1,26 +1,26 @@
|
|||||||
/**
|
// Copyright (c) 2023 Dominic Masters
|
||||||
* Copyright (c) 2021 Dominic Masters
|
//
|
||||||
*
|
// This software is released under the MIT License.
|
||||||
* This software is released under the MIT License.
|
// https://opensource.org/licenses/MIT
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "file.hpp"
|
#include "File.hpp"
|
||||||
|
|
||||||
void fileNormalizeSlashes(char *string) {
|
using namespace Dawn;
|
||||||
char c;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
while(c = string[i++]) {
|
std::string File::normalizeSlashes(std::string str) {
|
||||||
if(c != '\\' && c != '/') continue;
|
size_t i = 0;
|
||||||
string[i-1] = FILE_PATH_SEP;
|
while(i < str.size()) {
|
||||||
|
auto c = str[i];
|
||||||
|
if(c == '\\' || c == '/') str[i] = FILE_PATH_SEP;
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fileMkdirp(char *path) {
|
void File::mkdirp(std::string path) {
|
||||||
char buffer[FILENAME_MAX];
|
std::string buffer;
|
||||||
char c;
|
char c;
|
||||||
int i = 0;
|
size_t i = 0;
|
||||||
bool inFile;
|
bool inFile;
|
||||||
bool hasMore;
|
bool hasMore;
|
||||||
|
|
||||||
@@ -28,172 +28,166 @@ void fileMkdirp(char *path) {
|
|||||||
hasMore = false;
|
hasMore = false;
|
||||||
while(c = path[i]) {
|
while(c = path[i]) {
|
||||||
if((c == '\\' || c == '/') && i > 0) {
|
if((c == '\\' || c == '/') && i > 0) {
|
||||||
buffer[i] = '\0';
|
fileMkdir(buffer.c_str(), 0755);
|
||||||
fileMkdir(buffer, 0755);
|
|
||||||
inFile = false;
|
inFile = false;
|
||||||
hasMore = false;
|
hasMore = false;
|
||||||
buffer[i] = FILE_PATH_SEP;
|
buffer += FILE_PATH_SEP;
|
||||||
i++;
|
i++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(c == '.') inFile = true;
|
if(c == '.') inFile = true;
|
||||||
hasMore = true;
|
hasMore = true;
|
||||||
buffer[i] = c;
|
buffer += c;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!inFile && hasMore) {
|
if(!inFile && hasMore) {
|
||||||
buffer[i] = '\0';
|
fileMkdir(buffer.c_str(), 0755);
|
||||||
fileMkdir(buffer, 0755);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t assetReadString(FILE *file, char *buffer) {
|
//
|
||||||
size_t length;
|
|
||||||
fseek(file, 0, SEEK_END);// Seek to the end
|
File::File(std::string filename) {
|
||||||
length = ftell(file);// Get our current position (the end)
|
this->filename = File::normalizeSlashes(filename);
|
||||||
fseek(file, 0, SEEK_SET);// Reset the seek
|
|
||||||
if(buffer == NULL) return length;
|
|
||||||
return fread(buffer, 1, length, file);// Read all the bytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t readAhead(
|
bool_t File::open(enum FileMode mode) {
|
||||||
char *bufferIn, int32_t start,
|
assertNull(this->file);
|
||||||
char *bufferOut,
|
|
||||||
char *needles, int32_t needleCount
|
|
||||||
) {
|
|
||||||
int32_t i = start, k = 0, j;
|
|
||||||
char c;
|
|
||||||
bool needleFound = false;
|
|
||||||
|
|
||||||
if(bufferIn[i] == '\0') return 0;
|
this->mode = mode;
|
||||||
|
this->file = fopen(
|
||||||
|
this->filename.c_str(),
|
||||||
|
mode == FILE_MODE_READ ? "rb" : "wb"
|
||||||
|
);
|
||||||
|
|
||||||
while((c = bufferIn[i++]) != '\0') {
|
if(this->file == NULL) return false;
|
||||||
for(j = 0; j < needleCount; j++) {
|
|
||||||
if(c != needles[j]) continue;
|
|
||||||
needleFound = true;
|
|
||||||
}
|
|
||||||
if(needleFound) break;
|
|
||||||
if(bufferOut != NULL) bufferOut[k] = c;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(bufferOut != NULL) bufferOut[k] = '\0';
|
if(mode == FILE_MODE_READ) {
|
||||||
return k;
|
fseek(this->file, 0, SEEK_END);
|
||||||
}
|
this->length = ftell(this->file);
|
||||||
|
fseek(this->file, 0, SEEK_SET);
|
||||||
|
|
||||||
int32_t skipAhead(
|
if(this->length <= 0) {
|
||||||
char *bufferIn, int32_t start,
|
this->close();
|
||||||
char *needles, int32_t needleCount
|
|
||||||
) {
|
|
||||||
char c;
|
|
||||||
int32_t j, k = 0, i = start;
|
|
||||||
bool needleFound;
|
|
||||||
|
|
||||||
while((c = bufferIn[i++]) != '\0') {
|
|
||||||
needleFound = false;
|
|
||||||
for(j = 0; j < needleCount; j++) {
|
|
||||||
if(c != needles[j]) continue;
|
|
||||||
needleFound = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(!needleFound) break;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
return k;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fileGetDirectory(char *file, char* buffer) {
|
|
||||||
char *c, *p;
|
|
||||||
int32_t i;
|
|
||||||
p = strrchr(file, FILE_PATH_SEP);
|
|
||||||
c = file;
|
|
||||||
i = 0;
|
|
||||||
do {
|
|
||||||
buffer[i++] = *c;
|
|
||||||
} while(++c < p);
|
|
||||||
buffer[i] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fileListChildren(
|
|
||||||
char *directory,
|
|
||||||
char *buffer,
|
|
||||||
int32_t *count,
|
|
||||||
uint8_t *types,
|
|
||||||
char **children
|
|
||||||
) {
|
|
||||||
#if defined(_MSC_VER)
|
|
||||||
WIN32_FIND_DATA fdFile;
|
|
||||||
HANDLE hFind = NULL;
|
|
||||||
char sPath[2048];
|
|
||||||
int32_t i;
|
|
||||||
|
|
||||||
// Append wildcard
|
|
||||||
sprintf(sPath, "%s\\*.*", directory);
|
|
||||||
|
|
||||||
// Scan first
|
|
||||||
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) {
|
|
||||||
printf("Path not found: [%s]\n", directory);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this->length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Iterate
|
return true;
|
||||||
i = 0;
|
}
|
||||||
do {
|
|
||||||
if(
|
|
||||||
strcmp(fdFile.cFileName, ".") == 0 ||
|
|
||||||
strcmp(fdFile.cFileName, "..") == 0
|
|
||||||
) continue;
|
|
||||||
|
|
||||||
// Get Full path.
|
bool_t File::isOpen() {
|
||||||
sprintf(sPath, "%s\\%s", directory, fdFile.cFileName);
|
return this->file != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
//Is the entity a File or Folder?
|
bool_t File::exists() {
|
||||||
if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
if(this->file != nullptr) return true;
|
||||||
types[i] = FILE_CHILD_TYPE_DIR;
|
FILE *f = fopen(this->filename.c_str(), "rb");
|
||||||
|
if(f == NULL || f == nullptr) return false;
|
||||||
|
fclose(f);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void File::close() {
|
||||||
|
assertNotNull(this->file);
|
||||||
|
fclose(this->file);
|
||||||
|
this->file = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t File::mkdirp() {
|
||||||
|
File::mkdirp(this->filename);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t File::readString(std::string *out) {
|
||||||
|
assertNotNull(out);
|
||||||
|
|
||||||
|
if(!this->isOpen()) {
|
||||||
|
if(!this->open(FILE_MODE_READ)) return false;
|
||||||
|
}
|
||||||
|
assertTrue(this->mode == FILE_MODE_READ);
|
||||||
|
out->clear();
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
char buffer[FILE_BUFFER_SIZE + 1];// +1 for null term
|
||||||
|
while(i != this->length) {
|
||||||
|
size_t amt = mathMin<size_t>(FILE_BUFFER_SIZE, (this->length - i));
|
||||||
|
auto amtRead = fread(buffer, sizeof(char), amt, this->file);
|
||||||
|
if(amtRead != amt) return false;
|
||||||
|
i += amtRead;
|
||||||
|
buffer[amtRead] = '\0';
|
||||||
|
out->append(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t File::readAhead(char *buffer, size_t max, char needle) {
|
||||||
|
assertNotNull(buffer);
|
||||||
|
assertTrue(max > 0);
|
||||||
|
|
||||||
|
if(!this->isOpen()) {
|
||||||
|
if(!this->open(FILE_MODE_READ)) return 0;
|
||||||
|
}
|
||||||
|
assertTrue(this->mode == FILE_MODE_READ);
|
||||||
|
|
||||||
|
// Buffer
|
||||||
|
size_t pos = ftell(this->file);
|
||||||
|
size_t amountLeftToRead = mathMin<size_t>(max, this->length - pos);
|
||||||
|
char temporary[FILE_BUFFER_SIZE];
|
||||||
|
size_t n = 0;
|
||||||
|
|
||||||
|
while(amountLeftToRead > 0) {
|
||||||
|
size_t toRead = mathMin<size_t>(amountLeftToRead, FILE_BUFFER_SIZE);
|
||||||
|
amountLeftToRead -= toRead;
|
||||||
|
// Read bytes
|
||||||
|
size_t read = fread(temporary, sizeof(char), toRead, this->file);
|
||||||
|
|
||||||
|
// Read error?
|
||||||
|
if(toRead != read) return 0;
|
||||||
|
|
||||||
|
// Did we read the needle?
|
||||||
|
size_t i = 0;
|
||||||
|
while(i < read) {
|
||||||
|
char c = temporary[i++];
|
||||||
|
if(c == needle) {
|
||||||
|
return n;
|
||||||
} else {
|
} else {
|
||||||
types[i] = FILE_CHILD_TYPE_FILE;
|
buffer[n++] = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
children[i] = buffer + (i * FILE_CHILD_NAME_MAX);
|
|
||||||
strcpy(children[i], fdFile.cFileName);
|
|
||||||
i++;
|
|
||||||
} while(FindNextFile(hFind, &fdFile));
|
|
||||||
|
|
||||||
*count = i;
|
|
||||||
return true;
|
|
||||||
#else
|
|
||||||
struct dirent *de;
|
|
||||||
DIR *dr;
|
|
||||||
int32_t i;
|
|
||||||
|
|
||||||
// Open Dir
|
|
||||||
dr = opendir(directory);
|
|
||||||
if(dr == NULL) {
|
|
||||||
printf("Could not open directory");
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Needle was not found.
|
||||||
// Iterate
|
return -1;
|
||||||
i = 0;
|
|
||||||
while ((de = readdir(dr)) != NULL) {
|
|
||||||
// File or folder?
|
|
||||||
if(de->d_type != DT_REG) continue;
|
|
||||||
|
|
||||||
// Copy into child buffer
|
|
||||||
children[i] = buffer + (i * FILE_CHILD_NAME_MAX);
|
|
||||||
strcpy(children[i], de->d_name);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if(closedir(dr)) return false;
|
|
||||||
|
|
||||||
*count = i;
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string fileNormalizeSlashesNew(std::string str) {
|
bool_t File::writeString(std::string in) {
|
||||||
return str;
|
if(!this->isOpen()) {
|
||||||
|
if(!this->open(FILE_MODE_WRITE)) return false;
|
||||||
|
}
|
||||||
|
assertTrue(this->mode == FILE_MODE_WRITE);
|
||||||
|
return this->writeRaw((char *)in.c_str(), in.size()) && this->length == in.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t File::writeRaw(char *data, size_t len) {
|
||||||
|
if(!this->isOpen()) {
|
||||||
|
if(!this->open(FILE_MODE_WRITE)) return false;
|
||||||
|
}
|
||||||
|
assertTrue(this->mode == FILE_MODE_WRITE);
|
||||||
|
this->length = fwrite(data, sizeof(char_t), len, this->file);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void File::setPosition(size_t n) {
|
||||||
|
fseek(this->file, 0, SEEK_SET);
|
||||||
|
fseek(this->file, n, SEEK_CUR);
|
||||||
|
}
|
||||||
|
|
||||||
|
File::~File() {
|
||||||
|
if(this->file != nullptr) this->close();
|
||||||
}
|
}
|
||||||
+116
-52
@@ -1,17 +1,11 @@
|
|||||||
/**
|
// Copyright (c) 2023 Dominic Masters
|
||||||
* Copyright (c) 2021 Dominic Masters
|
//
|
||||||
*
|
// This software is released under the MIT License.
|
||||||
* This software is released under the MIT License.
|
// https://opensource.org/licenses/MIT
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "dawnsharedlibs.hpp"
|
#include "assert/assert.hpp"
|
||||||
|
#include "util/mathutils.hpp"
|
||||||
#define FILE_CHILD_TYPE_DIR 0x00
|
|
||||||
#define FILE_CHILD_TYPE_FILE 0x01
|
|
||||||
#define FILE_CHILD_NAME_MAX 512
|
|
||||||
#define FILE_CHILD_COUNT_MAX 64
|
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
@@ -26,51 +20,121 @@
|
|||||||
#define FILE_PATH_SEP '/'
|
#define FILE_PATH_SEP '/'
|
||||||
#define fileMkdir(path, perms) mkdir(path, perms)
|
#define fileMkdir(path, perms) mkdir(path, perms)
|
||||||
#endif
|
#endif
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
void fileNormalizeSlashes(char *string);
|
#define FILE_BUFFER_SIZE 512
|
||||||
|
|
||||||
void fileMkdirp(char *path);
|
namespace Dawn {
|
||||||
|
enum FileMode {
|
||||||
|
FILE_MODE_READ,
|
||||||
|
FILE_MODE_WRITE
|
||||||
|
};
|
||||||
|
|
||||||
size_t assetReadString(FILE *file, char *buffer);
|
class File {
|
||||||
|
private:
|
||||||
|
enum FileMode mode;
|
||||||
|
|
||||||
void fileGetDirectory(char *file, char* buffer);
|
public:
|
||||||
|
static std::string normalizeSlashes(std::string str);
|
||||||
|
static void mkdirp(std::string path);
|
||||||
|
|
||||||
bool fileListChildren(
|
std::string filename;
|
||||||
char *directory,
|
size_t length;
|
||||||
char *buffer,
|
FILE *file = nullptr;
|
||||||
int32_t *count,
|
|
||||||
uint8_t *types,
|
|
||||||
char **children
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads ahead to the first instance of the given character you provide.
|
* Constructs a new File interface class.
|
||||||
*
|
*
|
||||||
* @param bufferIn Buffer to scan.
|
* @param filename Filename that you want to interface with.
|
||||||
* @param start Start position within the buffer to scan from (inclusive).
|
*/
|
||||||
* @param bufferOut Where to write the temporary data that was read ahead.
|
File(std::string filename);
|
||||||
* @param needles Array of characters to scan for.
|
|
||||||
* @param needleCount How many elements are within the needles array.
|
|
||||||
* @return The count of characters skipped.
|
|
||||||
*/
|
|
||||||
int32_t readAhead(
|
|
||||||
char *bufferIn, int32_t start,
|
|
||||||
char *bufferOut,
|
|
||||||
char *needles, int32_t needleCount
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Skips any characters found in the needles.
|
* Opens a connection to the file.
|
||||||
*
|
*
|
||||||
* @param bufferIn Buffer of chars to read.
|
* @param mode File mode to use for this interface.
|
||||||
* @param start Start of the buffer.
|
* @return True if success, otherwise for failure.
|
||||||
* @param needles Needles you are trying to skip.
|
*/
|
||||||
* @param needleCount Count of needles in the needles array.
|
bool_t open(enum FileMode mode);
|
||||||
* @return The count of chars to skip ahead.
|
|
||||||
*/
|
|
||||||
int32_t skipAhead(
|
|
||||||
char *bufferIn, int32_t start,
|
|
||||||
char *needles, int32_t needleCount
|
|
||||||
);
|
|
||||||
|
|
||||||
static std::string fileNormalizeSlashesNew(std::string str);
|
/**
|
||||||
|
* Returns whether or not the file connection is currently opened.
|
||||||
|
*
|
||||||
|
* @return True if the connection is open, otherwise if it's not.
|
||||||
|
*/
|
||||||
|
bool_t isOpen();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether or not the file exists. Will open the connection if it
|
||||||
|
* does exist.
|
||||||
|
*
|
||||||
|
* @return True if exists, otherwsie if it doesn't.
|
||||||
|
*/
|
||||||
|
bool_t exists();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the currently open interface to the file. Done automatically
|
||||||
|
* when this object is disposed.
|
||||||
|
*/
|
||||||
|
void close();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes all directories above this file's filename.
|
||||||
|
*
|
||||||
|
* @return True if successful, otherwise false.
|
||||||
|
*/
|
||||||
|
bool_t mkdirp();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the entire contents of a file to the given output string buffer.
|
||||||
|
* This is a bit dangerous since the length of the file isn't checked
|
||||||
|
* against the memory to be consumed.
|
||||||
|
*
|
||||||
|
* @param out Pointer to a string where the output data will be stored.
|
||||||
|
* @return True if the read was successful, otherwise false.
|
||||||
|
*/
|
||||||
|
bool_t readString(std::string *out);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads ahead from the current position to a specific needle (character).
|
||||||
|
*
|
||||||
|
* @param buffer Buffer to output read chars to.
|
||||||
|
* @param max Max length of the buffer / amount of chars to read ahead.
|
||||||
|
* @param needle The character (needle) to look for.
|
||||||
|
* @return Amount of chars read, or <= 0 on error.
|
||||||
|
*/
|
||||||
|
size_t readAhead(
|
||||||
|
char *buffer,
|
||||||
|
size_t max,
|
||||||
|
char needle
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the entire contents of a string to a file.
|
||||||
|
*
|
||||||
|
* @param in String to write to this file.
|
||||||
|
* @return True if written successfully, otherwise false.
|
||||||
|
*/
|
||||||
|
bool_t writeString(std::string in);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write raw bytes to the file.
|
||||||
|
*
|
||||||
|
* @param data Data to write.
|
||||||
|
* @return True if written successfully, otherwise false.
|
||||||
|
*/
|
||||||
|
bool_t writeRaw(char *data, size_t );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the position of the cursor of the file reader.
|
||||||
|
*
|
||||||
|
* @param pos Position to set.
|
||||||
|
*/
|
||||||
|
void setPosition(size_t pos);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destruct the File manager.
|
||||||
|
*/
|
||||||
|
~File();
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user