Initial commit

This commit is contained in:
TSnake41 2020-02-27 00:14:24 +01:00
parent 667e5bea43
commit dae35f0d74
17 changed files with 12709 additions and 0 deletions

3
.clang_complete Normal file
View File

@ -0,0 +1,3 @@
-Iwren/src/include
-Iinclude
-Iraylib/src

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.o
wray_standalone\.exe
src/wray_api\.c
libwray\.a

0
.gitmodules vendored Normal file
View File

13
LICENCE Normal file
View File

@ -0,0 +1,13 @@
Copyright (C) 2020 Astie Teddy
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

1
luajit Submodule

@ -0,0 +1 @@
Subproject commit 0ad60ccbc3768fa8e3e726858adf261950edbc22

26
makefile Normal file
View File

@ -0,0 +1,26 @@
CFLAGS := -O2 -s
LDFLAGS := -O2 -s -lm -lluajit
AR ?= ar
LUA ?= luajit
BOOT_FILES := src/raylib.lua src/raylua.lua
all: raylua_s raylua_e
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
src/raylua_boot.c: src/raylib.lua src/raylua.lua
$(LUA) tools/lua2str.lua $@ raylua_boot $^
raylua_s: src/raylua_boot.o src/raylua_s.o
$(CC) -o $@ $^ $(LDFLAGS)
raylua_e: src/lib/miniz.o src/raylua_boot.o src/raylua_builder.o src/raylua_e.o
$(CC) -o $@ $^ -lwray $(LDFLAGS)
clean:
rm -rf raylua_s raylua_e src/raylua_boot.c src/*.o src/lib/miniz.o
.PHONY: raylua_s raylua_e

1224
src/lib/dirent.h Normal file

File diff suppressed because it is too large Load Diff

7657
src/lib/miniz.c Normal file

File diff suppressed because it is too large Load Diff

1338
src/lib/miniz.h Normal file

File diff suppressed because it is too large Load Diff

1020
src/raylib.lua Normal file

File diff suppressed because it is too large Load Diff

22
src/raylua.lua Normal file
View File

@ -0,0 +1,22 @@
local load = loadstring
if raylua then
-- Change the second loader to load files using raylua.loadfiles
package.loaders[2] = function (name)
for path in package.path:gmatch "([^;]+);?" do
path = path:gsub("?", name)
local status, content = raylua.loadfiles(path)
if status then
local f, err = load(content)
assert(f, err)
return f
end
end
return nil
end
end
require "main"

1043
src/raylua_boot.c Normal file

File diff suppressed because it is too large Load Diff

6
src/raylua_boot.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef H_RAYLUA_BOOT
#define H_RAYLUA_BOOT
extern const char *raylua_boot;
#endif /* H_RAYLUA_BOOT */

170
src/raylua_builder.c Normal file
View File

@ -0,0 +1,170 @@
/*
Copyright (C) 2019 Astie Teddy
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef WIN32
#include "lib/dirent.h"
#define stat _stat
#ifndef strcasecmp
#define strcasecmp strcmpi
#endif
#else
#include <dirent.h>
#endif
#include "lib/miniz.h"
#ifndef WRAY_NO_BUILDER
static void append_file(FILE *dst, FILE *src)
{
size_t count;
do {
uint8_t buffer[4096];
count = fread(buffer, 1, 4096, src);
fwrite(buffer, 1, count, dst);
} while(count == 4096);
}
int wray_build_executable(const char *self_path, const char *input_path)
{
printf("Create new executable from %s.\n", input_path);
struct stat st;
int result = stat(input_path, &st);
if (result) {
printf("%s: Can't get file information.\n", input_path);
return 1;
}
size_t arg_len = strlen(input_path);
size_t len = arg_len + 5; // + ".exe\0"
char output_path[len];
strcpy(output_path, input_path);
if (output_path[arg_len - 1] == '/') {
/* Remove trailing '/'. */
output_path[arg_len - 1] = '\0';
arg_len--;
}
strncpy(output_path + arg_len, ".exe", 5);
FILE *output = fopen(output_path, "wb");
if (output == NULL) {
printf("Can't open %s for writing.\n", output_path);
return 1;
}
FILE *self = fopen(self_path, "rb");
if (self == NULL) {
puts("Can't open itself");
return 1;
}
/* Copy self into output. */
append_file(output, self);
fclose(self);
/* Get the offset to put it at the end of the file. */
fpos_t offset;
fgetpos(output, &offset);
if (S_ISREG(st.st_mode)) {
/* Consider input as a bare bundle, just append file to get output. */
FILE *input = fopen(input_path, "rb");
append_file(output, input);
fclose(input);
} else if (S_ISDIR(st.st_mode)) {
/* We need to explore the directory and write each file to a zip file. */
mz_zip_archive zip;
mz_zip_zero_struct(&zip);
if (!mz_zip_writer_init_cfile(&zip, output, 0)) {
puts("Can't initialize zip writter inside output.");
return 0;
}
DIR *d = opendir(input_path);
chdir(input_path);
struct dirent *entry;
/* NOTE: Hardcoded 512 path limit. */
char dest_path[512];
while ((entry = readdir(d))) {
char *original_path = entry->d_name;
memset(dest_path, 0, 512);
struct stat st;
if (stat(original_path, &st)) {
printf("Skip %s (stat() failed)\n", original_path);
continue;
}
if (!S_ISREG(st.st_mode)) {
printf("Skip %s (not a file)\n", original_path);
continue;
}
size_t len = strlen(original_path);
if (len > 512) {
printf("Skipping %s: path too long\n", original_path);
continue;
}
if (len > 5 && strcmp(original_path + len - 5, ".wren") == 0) {
len -= 5; /* Exclude .wren extension. */
}
strncpy(dest_path, original_path, len);
printf("Add %s (original: %s)...\n", dest_path, original_path);
if (!mz_zip_writer_add_file(&zip, dest_path, original_path, NULL, 0,
MZ_BEST_COMPRESSION)) {
printf("miniz error: %x\n", mz_zip_get_last_error(&zip));
}
}
closedir(d);
puts("Finalizing zip archive...");
if (!mz_zip_writer_finalize_archive(&zip))
puts("Can't finalize archive.");
mz_zip_end(&zip);
// Write offset
fwrite(&offset, sizeof(fpos_t), 1, output);
fclose(output);
}
free(output_path);
return 0;
}
#endif

122
src/raylua_e.c Normal file
View File

@ -0,0 +1,122 @@
/*
Copyright (C) 2019 Astie Teddy
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Wray embedded executable */
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "raylua_boot.h"
#include "lib/miniz.h"
#ifndef WRAY_NO_BUILDER
int raylua_build_executable(const char *self_path, const char *input_path);
#endif
static mz_zip_archive zip_file;
int raylua_loadfile(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
int index = mz_zip_reader_locate_file(&zip_file, path, NULL, 0);
if (index == -1) {
lua_pushboolean(L, false);
lua_pushfstring(L, "%s: File not found.", path);
return 2;
}
mz_zip_archive_file_stat stat;
if (!mz_zip_reader_file_stat(&zip_file, index, &stat)) {
lua_pushboolean(L, false);
lua_pushfstring(L, "%s: Can't get file information.", path);
return 2;
}
size_t size = stat.m_uncomp_size;
char *buffer = malloc(size + 1);
if (buffer == NULL) {
lua_pushboolean(L, false);
lua_pushfstring(L, "%s: Can't allocate file buffer.", path);
return 2;
}
buffer[size] = '\0';
mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0);
lua_pushboolean(L, true);
lua_pushlstring(L, buffer, size);
free(buffer);
return 2;
}
int main(int argc, const char **argv)
{
mz_zip_zero_struct(&zip_file);
bool ready = false;
FILE *f = fopen(argv[0], "rb");
if (f != NULL) {
/* Read offset at the end of the file */
fpos_t offset;
fseek(f, -(long)sizeof(fpos_t), SEEK_END);
fread(&offset, sizeof(fpos_t), 1, f);
fsetpos(f, &offset);
if (mz_zip_reader_init_cfile(&zip_file, f, 0, 0))
ready = true;
}
if (!ready) {
if (argc < 2) {
puts("Usage: raylua_e <input>");
return 0;
} else
return raylua_build_executable(argv[0], argv[1]);
}
lua_State *L = luaL_newstate();
if (L == NULL)
puts("[RAYLUA] Unable to initialize Lua.");
lua_newtable(L);
int i = 0;
while (argc != i) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
i++;
}
lua_setglobal(L, "arg");
lua_pushcfunction(L, raylua_loadfile);
lua_setglobal(L, "raylua_loadfile");
luaL_dostring(L, raylua_boot);
lua_close(L);
return 0;
}

49
src/raylua_s.c Normal file
View File

@ -0,0 +1,49 @@
/*
Copyright (C) 2019-2020 Astie Teddy
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "raylua_boot.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int main(int argc, const char **argv)
{
lua_State *L = luaL_newstate();
if (L == NULL)
puts("[RAYLUA] Unable to initialize Lua.");
lua_newtable(L);
int i = 0;
while (argc != i) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
i++;
}
lua_setglobal(L, "arg");
luaL_dostring(L, raylua_boot);
lua_close(L);
return 0;
}

11
tools/lua2str.lua Normal file
View File

@ -0,0 +1,11 @@
local output = io.open(arg[1], "w")
output:write("const char *" .. arg[2] .. " = ")
for i=3,#arg do
for line in io.lines(arg[i]) do
output:write(string.format('%q "\\n"', line) .. '\n')
end
end
output:write ";"
output:close()