Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimizations, colored output, better help, and more flags. #17

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Alex313031
Copy link

@Alex313031 Alex313031 commented Mar 28, 2023

From > https://github.com/Alex313031/thorium/tree/main/pak_src

Also includes a build script for linux.

@myfreeer myfreeer self-assigned this Mar 28, 2023
Copy link
Owner

@myfreeer myfreeer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for your contribution, see comments in the code for more detail.

@@ -7,16 +7,78 @@
#include "pak_get_file_type.h"
#include "pak_header.h"
#include "pak_pack.h"
#include <stdio.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdio.h is already included in pak_file_io.h by pak_pack.h.

@@ -7,16 +7,78 @@
#include "pak_get_file_type.h"
#include "pak_header.h"
#include "pak_pack.h"
#include <stdio.h>

void red () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short functions could be marked as inline to give compiler a better hint to inline it.

#include <stdio.h>

void red () {
printf("\033[1;31m");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider 3 more things here:

  1. For printing s static string, puts(char*) with a new line and `fputs(char*, stdout) without a new line is preferred [1]
  2. These strings (this and similar strings below) is used many times, defining it as macros makes literials more clear to read and mainten.
  3. Colored output are not always enabled for all kinds of terminals, for example, a docker run without the -t option, default behavior should detect support whenever possible, and there should be options to force enable or disable them. Also, a compile-time option could be also used in case packagers want to disable this feature without patching the code.

Extra links for 1:
https://stackoverflow.com/questions/5690979/when-should-i-use-fputs-instead-of-fprintf
https://stackoverflow.com/questions/17238784/c-puts-without-newline
https://stackoverflow.com/questions/2454474/what-is-the-difference-between-printf-and-puts-in-c

Images for 2, using the ci compiled pak_mingw64.exe:
image
image


void printVersion() {
// get self path
char selfName[PATH_MAX];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These code seems not used below

if (ptr != NULL)
strcpy(selfName, ptr + 1);

printf(PAK_VERSION_STRING);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. puts and fputs is perferred.
  2. The standalone switch for version should print sonething readable by machine, human-readable stuff can go with the help text.
  3. Versions should be defined as macro so it can be easily updated when needed.


void printChromium() {
// get self path
char selfName[PATH_MAX];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like mentioned above, the path of the exe seems not needed here.

if (ptr != NULL)
strcpy(selfName, ptr + 1);

printf(CHROMIUM_ASCII);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be an easter egg, but this takes more than 1kB of a ~16kB program, could this be optional by a compile-time flag?

@@ -121,12 +232,15 @@ int pakPackIndexFile(char *indexPath, char *outputFilePath) {
free(outputFilePath2);
if (freeFilesPath && filesPath != NULL)
free(filesPath);
printf("\033[1;32m\nPacked %s\033[0m\n\n", outputFilePath);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the indent of this file

printf("\npak size: %u\n", pakFile.size);
printf("version = %u\nencoding = %u\n", myHeader.version, myHeader.encoding);
printf("\n.pak size: %u", pakFile.size);
printf(" bytes\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This printf could be merged into the printf above.

@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 2.8)
project(chrome-pak C)
cmake_minimum_required(VERSION 3.0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would upgrading minimum version of cmake a good idea? Are there any features used in this pr require to do this? Maybe the cmake version or the existance of the policy could be checked before applying a policy.

Link: https://cmake.org/cmake/help/v2.8.12/cmake.html#command:if

@Alex313031
Copy link
Author

@myfreeer Im really f***king dumb at C. It took me two hours to cobble this together. Would you be willing to make the necessary changes? Then you could send a pull request back to me or something

@myfreeer
Copy link
Owner

myfreeer commented Apr 2, 2023

Here is an example of using macros for colored output:

https://github.com/recp/cglm/blob/92a8e3816275ecfbebfbaec5af6e4fce4e6f9b84/test/include/common.h#L51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants