openmohaa/code/skeletor/skeletor_utilities.cpp

325 lines
6.9 KiB
C++
Raw Permalink Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
Copyright (C) 2024 the OpenMoHAA team
2016-03-27 11:49:47 +02:00
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under 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.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// skeletor.cpp : Skeletor
#include "q_shared.h"
#include "skeletor.h"
2023-11-06 17:58:58 +01:00
void SKEL_Message(const char *fmt, ...)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
static char msg[32000];
va_list va;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
va_start(va, fmt);
2024-09-20 21:53:48 +02:00
Q_vsnprintf(msg, sizeof(msg), fmt, va);
2023-11-06 17:58:58 +01:00
va_end(va);
Skel_DPrintf(msg);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
void SKEL_Warning(const char *fmt, ...)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
char msg[1024];
va_list va;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
va_start(va, fmt);
2024-09-20 21:53:48 +02:00
Q_vsnprintf(msg, sizeof(msg), fmt, va);
2023-11-06 17:58:58 +01:00
va_end(va);
Skel_DPrintf(msg);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
void SKEL_Error(const char *fmt, ...)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
char msg[1024];
va_list va;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
va_start(va, fmt);
2024-09-20 21:53:48 +02:00
Q_vsnprintf(msg, sizeof(msg), fmt, va);
2023-11-06 17:58:58 +01:00
va_end(va);
Skel_DPrintf(msg);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
int FileLength(FILE *pFile)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
int iPos;
int iEnd;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
iPos = ftell(pFile);
fseek(pFile, 0, SEEK_END);
iEnd = ftell(pFile);
fseek(pFile, iPos, SEEK_SET);
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
return iEnd;
2016-03-27 11:49:47 +02:00
}
int skelChannelList_s::GlobalChannel(int localchannel) const
{
assert(localchannel < MAX_GLOBAL_FROM_LOCAL);
return m_chanGlobalFromLocal[localchannel];
}
int skelChannelList_s::LocalChannel(int channel) const
{
return GetLocalFromGlobal(channel);
}
2023-11-06 17:58:58 +01:00
int skelChannelList_s::GetLocalFromGlobal(int globalChannel) const
2016-03-27 11:49:47 +02:00
{
if (!m_chanLocalFromGlobal) {
2023-11-06 17:58:58 +01:00
return -1;
}
if (globalChannel >= m_numLocalFromGlobal) {
return -1;
}
return m_chanLocalFromGlobal[globalChannel];
2016-03-27 11:49:47 +02:00
}
void skelChannelList_s::SetLocalFromGlobal(int channel, int localchannel)
{
m_chanLocalFromGlobal[channel] = localchannel;
}
2023-11-06 17:58:58 +01:00
int skelChannelList_s::NumChannels(void) const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
return m_numChannels;
2016-03-27 11:49:47 +02:00
}
void skelChannelList_s::ZeroChannels()
{
2023-11-06 17:58:58 +01:00
int i;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
m_numChannels = 0;
m_chanLocalFromGlobal = (short *)Skel_Alloc(MAX_SKELETOR_CHANNELS * sizeof(short));
m_numLocalFromGlobal = MAX_SKELETOR_CHANNELS;
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
for (i = 0; i < MAX_SKELETOR_CHANNELS; i++) {
m_chanLocalFromGlobal[i] = -1;
}
2016-03-27 11:49:47 +02:00
}
void skelChannelList_s::PackChannels()
{
2023-11-06 17:58:58 +01:00
m_numLocalFromGlobal = MAX_SKELETOR_CHANNELS - 1;
for (m_numLocalFromGlobal = MAX_SKELETOR_CHANNELS - 1; m_numLocalFromGlobal >= 0; m_numLocalFromGlobal--) {
if (m_chanLocalFromGlobal[m_numLocalFromGlobal] != -1) {
break;
}
2023-11-06 17:58:58 +01:00
}
if (m_numLocalFromGlobal < MAX_SKELETOR_CHANNELS) {
m_numLocalFromGlobal++;
}
if (m_numLocalFromGlobal > 0) {
2023-11-06 17:58:58 +01:00
short *old_array = m_chanLocalFromGlobal;
int i;
2023-11-06 17:58:58 +01:00
m_chanLocalFromGlobal = (short *)Skel_Alloc(m_numLocalFromGlobal * sizeof(m_chanLocalFromGlobal[0]));
for (i = 0; i < m_numLocalFromGlobal; i++) {
m_chanLocalFromGlobal[i] = old_array[i];
}
2023-11-06 17:58:58 +01:00
Skel_Free(old_array);
} else {
Skel_Free(m_chanLocalFromGlobal);
m_chanLocalFromGlobal = NULL;
m_numLocalFromGlobal = -1;
2023-11-06 17:58:58 +01:00
}
2016-03-27 11:49:47 +02:00
}
void skelChannelList_s::CleanUpChannels()
{
2023-11-06 17:58:58 +01:00
if (m_chanLocalFromGlobal) {
Skel_Free(m_chanLocalFromGlobal);
m_chanLocalFromGlobal = NULL;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
m_numLocalFromGlobal = 0;
2016-03-27 11:49:47 +02:00
}
void skelChannelList_s::InitChannels()
{
2023-11-06 17:58:58 +01:00
m_chanLocalFromGlobal = NULL;
m_numLocalFromGlobal = 0;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
int skelChannelList_s::AddChannel(int newGlobalChannelNum)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
short int iLocalChannel;
if (newGlobalChannelNum == -1) {
// Fixed in 2.0
// Set the global from local to -1
m_chanGlobalFromLocal[m_numChannels] = -1;
return m_numChannels++;
}
iLocalChannel = GetLocalFromGlobal(newGlobalChannelNum);
if (iLocalChannel < 0) {
iLocalChannel = m_numChannels++;
m_chanGlobalFromLocal[iLocalChannel] = newGlobalChannelNum;
SetLocalFromGlobal(newGlobalChannelNum, iLocalChannel);
2023-11-06 17:58:58 +01:00
}
return iLocalChannel;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
qboolean skelChannelList_s::HasChannel(ChannelNameTable *nameTable, const char *channelName) const
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
short int iGlobalChannel = nameTable->FindNameLookup(channelName);
2024-06-10 23:40:34 +02:00
return iGlobalChannel >= 0 && LocalChannel(iGlobalChannel) >= 0;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
const char *skelChannelList_s::ChannelName(ChannelNameTable *nameTable, int localChannelNum) const
2016-03-27 11:49:47 +02:00
{
2024-06-10 23:40:34 +02:00
if (localChannelNum >= this->m_numChannels || localChannelNum < 0) {
2023-11-06 17:58:58 +01:00
return NULL;
}
2024-06-10 23:40:34 +02:00
if (m_chanGlobalFromLocal[localChannelNum] < 0) {
return 0;
}
return nameTable->FindName(m_chanGlobalFromLocal[localChannelNum]);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
void Skel_ExtractFilePath(const char *path, char *dest)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
const char *src = path + strlen(path);
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
while (src != path) {
if (*(src - 1) == '\\') {
break;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
if (*(src - 1) == '/') {
break;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
src--;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
memcpy(dest, src, src - path);
dest[src - path] = 0;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
void Skel_ExtractFileBase(const char *path, char *dest)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
const char *src = path + strlen(path);
const char *dot = src;
while (src != path) {
if (*(src - 1) == '\\') {
break;
}
if (*(src - 1) == '/') {
break;
}
src--;
}
while (dot != src) {
if (*(dot - 1) != '.') {
break;
}
dot--;
}
if (dot == src) {
*dest = 0;
return;
}
memcpy(dest, src, dot - src);
dest[dot - src] = 0;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
void Skel_ExtractFileExtension(const char *path, char *dest)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
const char *src = path + strlen(path);
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
while (src != path) {
if (*(src - 1) == '.') {
break;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
src--;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
if (src == path) {
*dest = 0;
return;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
strcpy(dest, src);
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
const char *Skel_ExtractFileExtension(const char *in)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
static char exten[8];
int i;
for (i = 0; in[i] != 0; i++) {
if (in[i] == '.') {
i++;
break;
}
}
if (!in[i]) {
return "";
}
strncpy(exten, &in[i], sizeof(exten));
return exten;
2016-03-27 11:49:47 +02:00
}
2023-11-06 17:58:58 +01:00
void Skel_ExtractFileName(const char *path, char *dest)
2016-03-27 11:49:47 +02:00
{
2023-11-06 17:58:58 +01:00
const char *src = path + strlen(path);
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
while (src != path) {
if (*(src - 1) == '\\') {
break;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
if (*(src - 1) == '/') {
break;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
src--;
}
2016-03-27 11:49:47 +02:00
2023-11-06 17:58:58 +01:00
strcpy(dest, src);
2016-03-27 11:49:47 +02:00
}