mirror of
https://github.com/ZDoom/ZDRay.git
synced 2024-11-21 19:50:54 +00:00
Add back BinFile
This commit is contained in:
parent
e9758d8d5f
commit
df010a5643
3 changed files with 224 additions and 0 deletions
|
@ -11,6 +11,8 @@ set(ZDRAY_SOURCES
|
|||
src/commandline/getopt.c
|
||||
src/commandline/getopt1.c
|
||||
src/commandline/getopt.h
|
||||
src/framework/binfile.cpp
|
||||
src/framework/binfile.h
|
||||
src/framework/halffloat.cpp
|
||||
src/framework/zstring.cpp
|
||||
src/framework/zstrformat.cpp
|
||||
|
|
161
src/framework/binfile.cpp
Normal file
161
src/framework/binfile.cpp
Normal file
|
@ -0,0 +1,161 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Note: this is a modified version of dlight. It is not the original software.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2013-2014 Samuel Villarreal
|
||||
// svkaiser@gmail.com
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source
|
||||
// distribution.
|
||||
//
|
||||
|
||||
#include "framework/binfile.h"
|
||||
|
||||
typedef union
|
||||
{
|
||||
int i;
|
||||
float f;
|
||||
} fint_t;
|
||||
|
||||
uint8_t BinFile::Read8()
|
||||
{
|
||||
uint8_t result;
|
||||
result = buffer[bufferOffset++];
|
||||
return result;
|
||||
}
|
||||
|
||||
short BinFile::Read16()
|
||||
{
|
||||
int result;
|
||||
result = Read8();
|
||||
result |= Read8() << 8;
|
||||
return result;
|
||||
}
|
||||
|
||||
int BinFile::Read32()
|
||||
{
|
||||
int result;
|
||||
result = Read8();
|
||||
result |= Read8() << 8;
|
||||
result |= Read8() << 16;
|
||||
result |= Read8() << 24;
|
||||
return result;
|
||||
}
|
||||
|
||||
float BinFile::ReadFloat()
|
||||
{
|
||||
fint_t fi;
|
||||
fi.i = Read32();
|
||||
return fi.f;
|
||||
}
|
||||
|
||||
FVector3 BinFile::ReadVector()
|
||||
{
|
||||
FVector3 vec;
|
||||
|
||||
vec.X = ReadFloat();
|
||||
vec.Y = ReadFloat();
|
||||
vec.Z = ReadFloat();
|
||||
|
||||
return vec;
|
||||
}
|
||||
|
||||
std::string BinFile::ReadString()
|
||||
{
|
||||
std::string str;
|
||||
char c = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (!(c = Read8()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
str += c;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
void BinFile::Write8(const uint8_t val)
|
||||
{
|
||||
buffer[bufferOffset] = val;
|
||||
bufferOffset++;
|
||||
}
|
||||
|
||||
void BinFile::Write16(const short val)
|
||||
{
|
||||
Write8(val & 0xff);
|
||||
Write8((val >> 8) & 0xff);
|
||||
}
|
||||
|
||||
void BinFile::Write32(const int val)
|
||||
{
|
||||
Write8(val & 0xff);
|
||||
Write8((val >> 8) & 0xff);
|
||||
Write8((val >> 16) & 0xff);
|
||||
Write8((val >> 24) & 0xff);
|
||||
}
|
||||
|
||||
void BinFile::WriteFloat(const float val)
|
||||
{
|
||||
fint_t fi;
|
||||
fi.f = val;
|
||||
Write32(fi.i);
|
||||
}
|
||||
|
||||
void BinFile::WriteVector(const FVector3 &val)
|
||||
{
|
||||
WriteFloat(val.X);
|
||||
WriteFloat(val.Y);
|
||||
WriteFloat(val.Z);
|
||||
}
|
||||
|
||||
void BinFile::WriteString(const std::string &val)
|
||||
{
|
||||
const char *c = val.c_str();
|
||||
|
||||
for (size_t i = 0; i < val.size(); i++)
|
||||
{
|
||||
Write8(c[i]);
|
||||
}
|
||||
|
||||
Write8(0);
|
||||
}
|
||||
|
||||
int BinFile::GetOffsetValue(int id)
|
||||
{
|
||||
return *(int*)(buffer + (id << 2));
|
||||
}
|
||||
|
||||
uint8_t *BinFile::GetOffset(int id, uint8_t *subdata, int *count)
|
||||
{
|
||||
uint8_t *data = (subdata == nullptr) ? buffer : subdata;
|
||||
|
||||
bufferOffset = GetOffsetValue(id);
|
||||
uint8_t *dataOffs = &data[bufferOffset];
|
||||
|
||||
if (count)
|
||||
{
|
||||
*count = *(int*)(dataOffs);
|
||||
}
|
||||
|
||||
return dataOffs;
|
||||
}
|
61
src/framework/binfile.h
Normal file
61
src/framework/binfile.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Note: this is a modified version of dlight. It is not the original software.
|
||||
//-----------------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2013-2014 Samuel Villarreal
|
||||
// svkaiser@gmail.com
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source
|
||||
// distribution.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "vectors.h"
|
||||
#include <string>
|
||||
|
||||
class BinFile
|
||||
{
|
||||
public:
|
||||
uint8_t Read8();
|
||||
short Read16();
|
||||
int Read32();
|
||||
float ReadFloat();
|
||||
FVector3 ReadVector();
|
||||
std::string ReadString();
|
||||
|
||||
void Write8(const uint8_t val);
|
||||
void Write16(const short val);
|
||||
void Write32(const int val);
|
||||
void WriteFloat(const float val);
|
||||
void WriteVector(const FVector3 &val);
|
||||
void WriteString(const std::string &val);
|
||||
|
||||
int GetOffsetValue(int id);
|
||||
uint8_t *GetOffset(int id, uint8_t *subdata = nullptr, int *count = nullptr);
|
||||
|
||||
uint8_t *Buffer() const { return buffer; }
|
||||
void SetBuffer(uint8_t *ptr) { buffer = ptr; }
|
||||
uint8_t *BufferAt() const { return &buffer[bufferOffset]; }
|
||||
void SetOffset(const int offset) { bufferOffset = offset; }
|
||||
|
||||
private:
|
||||
uint8_t *buffer = nullptr;
|
||||
unsigned int bufferOffset = 0;
|
||||
};
|
Loading…
Reference in a new issue