Skip to content

Commit

Permalink
Modularize skap_header to its own component (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed May 23, 2024
1 parent 94cbe8c commit 50967db
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 39 deletions.
27 changes: 27 additions & 0 deletions tools/skap/include/skap_defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky 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 3 of the License, or
* (at your option) any later version.
*
* Sparky 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 Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <stdint.h>

typedef uint8_t u8;
typedef uint64_t u64;
32 changes: 32 additions & 0 deletions tools/skap/include/skap_header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky 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 3 of the License, or
* (at your option) any later version.
*
* Sparky 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 Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <skap_defines.h>

typedef struct {
char signature[4];
u8 fmt_ver;
u64 build_ver;
} skap_header;

skap_header skap_header_create(void);
41 changes: 2 additions & 39 deletions tools/skap/src/skap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,11 @@
*/


#include <time.h>
#include <stdio.h>
#include <stdint.h>

typedef uint8_t u8;
typedef uint64_t u64;

typedef struct {
char signature[4];
u8 fmt_ver;
u64 build_ver;
} skap_header;

static u64 concat_num(u64 x, u64 y) {
u64 n = 10;
while (y >= n) n *= 10;
return n * x + y;
}

static u64 compute_build_ver(void) {
time_t t = time(0);
struct tm *t_spec = localtime(&t);
u64 t_spec_arr[] = {
t_spec->tm_mon + 1,
t_spec->tm_mday,
t_spec->tm_hour,
t_spec->tm_min
};
u64 build_ver = t_spec->tm_year + 1900;
for (size_t i = 0; i < 4; ++i) {
if (t_spec_arr[i] < 10) build_ver *= 10;
build_ver = concat_num(build_ver, t_spec_arr[i]);
}
return build_ver;
}
#include <skap_header.h>

int main(void) {
skap_header header = {
.signature = { 'S', 'K', 'A', 'P' },
.fmt_ver = 1,
.build_ver = compute_build_ver()
};
skap_header header = skap_header_create();
printf("header.signature => %s\n", header.signature);
printf("header.fmt_ver => %hhu\n", header.fmt_ver);
printf("header.build_ver => %ld\n", header.build_ver);
Expand Down
54 changes: 54 additions & 0 deletions tools/skap/src/skap_header.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky 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 3 of the License, or
* (at your option) any later version.
*
* Sparky 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 Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <time.h>
#include <skap_header.h>

static u64 concat_num(u64 x, u64 y) {
u64 n = 10;
while (y >= n) n *= 10;
return n * x + y;
}

static u64 compute_build_ver(void) {
time_t t = time(0);
struct tm *t_spec = localtime(&t);
u64 t_spec_arr[] = {
t_spec->tm_mon + 1,
t_spec->tm_mday,
t_spec->tm_hour,
t_spec->tm_min
};
u64 build_ver = t_spec->tm_year + 1900;
for (size_t i = 0; i < 4; ++i) {
if (t_spec_arr[i] < 10) build_ver *= 10;
build_ver = concat_num(build_ver, t_spec_arr[i]);
}
return build_ver;
}

skap_header skap_header_create(void) {
return (skap_header) {
.signature = { 'S', 'K', 'A', 'P' },
.fmt_ver = 1,
.build_ver = compute_build_ver()
};
}

0 comments on commit 50967db

Please sign in to comment.