remove makefile support, project restructure

This commit is contained in:
Protryon 2019-04-14 13:45:32 -07:00
parent af9a444c76
commit ee0e6ea5a2
92 changed files with 649 additions and 2744 deletions

View File

@ -1,83 +1,26 @@
cmake_minimum_required(VERSION 3.6)
project(Basin)
project(basin)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 11)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/basin/Debug)
option(JAVA_PLUGIN_SUPPORT "Enables Java plugin support, requires JAVA_HOME variable" ON)
set(SOURCE_FILES
basin/src/accept.c
basin/src/accept.h
basin/src/ai.c
basin/src/ai.h
basin/src/anticheat.c
basin/src/anticheat.h
basin/src/basin.h
basin/src/biome.h
basin/src/block.c
basin/src/block.h
basin/src/collection.c
basin/src/collection.h
basin/src/command.c
basin/src/command.h
basin/src/config.c
basin/src/config.h
basin/src/crafting.c
basin/src/crafting.h
basin/src/entity.c
basin/src/entity.h
basin/src/game.c
basin/src/game.h
basin/src/globals.h
basin/src/hashmap.c
basin/src/hashmap.h
basin/src/inventory.c
basin/src/inventory.h
basin/src/item.c
basin/src/item.h
basin/src/json.c
basin/src/json.h
basin/src/log.c
basin/src/log.h
basin/src/main.c
basin/src/nbt.c
basin/src/nbt.h
basin/src/network.c
basin/src/network.h
basin/src/packet.c
basin/src/packet.h
basin/src/perlin.c
basin/src/perlin.h
basin/src/player.c
basin/src/player.h
basin/src/plugin.c
basin/src/plugin.h
basin/src/profile.c
basin/src/profile.h
basin/src/queue.c
basin/src/queue.h
basin/src/server.h
basin/src/smelting.c
basin/src/smelting.h
basin/src/streams.c
basin/src/streams.h
basin/src/tileentity.c
basin/src/tileentity.h
basin/src/tools.c
basin/src/tools.h
basin/src/util.c
basin/src/util.h
basin/src/version.h
basin/src/work.c
basin/src/work.h
basin/src/world.c
basin/src/world.h
basin/src/worldmanager.c
basin/src/worldmanager.h
basin/src/xstring.c
basin/src/xstring.h)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-discarded-qualifiers")
add_executable(Basin ${SOURCE_FILES})
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
file(GLOB src
"src/*.c"
"src/*.h"
)
add_executable(basin ${src})
target_link_libraries(basin pthread crypto ssl m z dl avuna-util)
target_include_directories(basin PUBLIC include/)
if (JAVA_PLUGIN_SUPPORT)
target_link_libraries(basin jvm)
target_include_directories(basin PRIVATE $ENV{JAVA_HOME}/include/)
endif()
target_link_libraries(Basin pthread crypto ssl m z dl)

View File

@ -1,92 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="cdt.managedbuild.config.gnu.exe.debug.1769467163">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.exe.debug.1769467163" moduleId="org.eclipse.cdt.core.settings" name="Debug">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" errorParsers="org.eclipse.cdt.core.GASErrorParser;org.eclipse.cdt.core.GmakeErrorParser;org.eclipse.cdt.core.GLDErrorParser;org.eclipse.cdt.core.CWDLocator;org.eclipse.cdt.core.GCCErrorParser" id="cdt.managedbuild.config.gnu.exe.debug.1769467163" name="Debug" parent="cdt.managedbuild.config.gnu.exe.debug">
<folderInfo id="cdt.managedbuild.config.gnu.exe.debug.1769467163." name="/" resourcePath="">
<toolChain errorParsers="" id="cdt.managedbuild.toolchain.gnu.exe.debug.1649192409" name="Linux GCC" superClass="cdt.managedbuild.toolchain.gnu.exe.debug">
<targetPlatform binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.target.gnu.platform.exe.debug.753514033" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.exe.debug"/>
<builder buildPath="${workspace_loc:/basin}/Debug" errorParsers="org.eclipse.cdt.core.GmakeErrorParser;org.eclipse.cdt.core.CWDLocator" id="cdt.managedbuild.target.gnu.builder.exe.debug.1034178310" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.exe.debug"/>
<tool id="cdt.managedbuild.tool.gnu.archiver.base.1610722987" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug.18882949" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.exe.debug">
<option id="gnu.cpp.compiler.exe.debug.option.optimization.level.8026142" name="Optimization Level" superClass="gnu.cpp.compiler.exe.debug.option.optimization.level" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
<option id="gnu.cpp.compiler.exe.debug.option.debugging.level.1625287618" name="Debug Level" superClass="gnu.cpp.compiler.exe.debug.option.debugging.level" value="gnu.cpp.compiler.debugging.level.max" valueType="enumerated"/>
</tool>
<tool errorParsers="org.eclipse.cdt.core.GCCErrorParser" id="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.287902410" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.debug">
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.exe.debug.option.optimization.level.85127222" name="Optimization Level" superClass="gnu.c.compiler.exe.debug.option.optimization.level" valueType="enumerated"/>
<option id="gnu.c.compiler.exe.debug.option.debugging.level.1202660524" name="Debug Level" superClass="gnu.c.compiler.exe.debug.option.debugging.level" value="gnu.c.debugging.level.max" valueType="enumerated"/>
<option id="gnu.c.compiler.option.dialect.std.1846139639" name="Language standard" superClass="gnu.c.compiler.option.dialect.std" value="gnu.c.compiler.dialect.default" valueType="enumerated"/>
<option id="gnu.c.compiler.option.dialect.flags.767216746" name="Other dialect flags" superClass="gnu.c.compiler.option.dialect.flags" value="-std=gnu11" valueType="string"/>
<option id="gnu.c.compiler.option.include.paths.1611456919" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include/&quot;"/>
<listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include/linux/&quot;"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.929664948" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool errorParsers="org.eclipse.cdt.core.GLDErrorParser" id="cdt.managedbuild.tool.gnu.c.linker.exe.debug.1069626255" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.exe.debug">
<option id="gnu.c.link.option.libs.627461760" name="Libraries (-l)" superClass="gnu.c.link.option.libs" valueType="libs">
<listOptionValue builtIn="false" value="pthread"/>
<listOptionValue builtIn="false" value="crypto"/>
<listOptionValue builtIn="false" value="ssl"/>
<listOptionValue builtIn="false" value="m"/>
<listOptionValue builtIn="false" value="z"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.linker.input.1626314517" superClass="cdt.managedbuild.tool.gnu.c.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.exe.debug.940105122" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.exe.debug"/>
<tool errorParsers="org.eclipse.cdt.core.GASErrorParser" id="cdt.managedbuild.tool.gnu.assembler.exe.debug.69795815" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.exe.debug">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.161983121" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
</toolChain>
</folderInfo>
<fileInfo id="cdt.managedbuild.config.gnu.exe.debug.1769467163.805022634" name="plugin.c" rcbsApplicability="disable" resourcePath="src/plugin.c" toolsToInvoke="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.287902410.734747101">
<tool id="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.287902410.734747101" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.287902410">
<option id="gnu.c.compiler.option.include.paths.1420764305" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/jre/lib/amd64/server/&quot;"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.74578801" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
</fileInfo>
<sourceEntries>
<entry excluding="src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="basin.cdt.managedbuild.target.gnu.exe.1827689322" name="Executable" projectType="cdt.managedbuild.target.gnu.exe"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.release.1519534979;cdt.managedbuild.config.gnu.exe.release.1519534979.;cdt.managedbuild.tool.gnu.c.compiler.exe.release.1793074859;cdt.managedbuild.tool.gnu.c.compiler.input.1006241285">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.exe.debug.1769467163;cdt.managedbuild.config.gnu.exe.debug.1769467163.;cdt.managedbuild.tool.gnu.c.compiler.exe.debug.287902410;cdt.managedbuild.tool.gnu.c.compiler.input.929664948">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope" versionNumber="2">
<configuration configurationName="Debug">
<resource resourceType="PROJECT" workspacePath="/basin"/>
</configuration>
</storageModule>
</cproject>

View File

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>basin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>

View File

@ -1,6 +0,0 @@
eclipse.preferences.version=1
environment/project/cdt.managedbuild.config.gnu.exe.debug.1769467163/JAVA_HOME/delimiter=\:
environment/project/cdt.managedbuild.config.gnu.exe.debug.1769467163/JAVA_HOME/operation=append
environment/project/cdt.managedbuild.config.gnu.exe.debug.1769467163/JAVA_HOME/value=/usr/lib/jvm/java-8-oracle
environment/project/cdt.managedbuild.config.gnu.exe.debug.1769467163/append=true
environment/project/cdt.managedbuild.config.gnu.exe.debug.1769467163/appendContributed=true

View File

@ -1,8 +0,0 @@
[Bb]asin
src/*.o
src/*.d
world
world.old
world_nether
world_the_end
plugins/*

View File

@ -1,40 +0,0 @@
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: basin
# Tool invocations
basin: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C Linker'
$(CC) $(CFLAGS) -L${JAVA_HOME}/jre/lib/amd64/server/ -rdynamic -o "basin" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) basin
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets

View File

@ -1,8 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
USER_OBJS :=
LIBS := -lpthread -lcrypto -lssl -lm -lz -ldl -ljvm -lzip

View File

@ -1,17 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
OBJ_SRCS :=
ASM_SRCS :=
C_SRCS :=
O_SRCS :=
S_UPPER_SRCS :=
EXECUTABLES :=
OBJS :=
C_DEPS :=
# Every subdirectory with source files must be described here
SUBDIRS := \
src \

View File

@ -1,119 +0,0 @@
# Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../src/accept.c \
../src/block.c \
../src/collection.c \
../src/command.c \
../src/config.c \
../src/crafting.c \
../src/entity.c \
../src/game.c \
../src/inventory.c \
../src/item.c \
../src/json.c \
../src/log.c \
../src/main.c \
../src/nbt.c \
../src/network.c \
../src/packet.c \
../src/player.c \
../src/queue.c \
../src/smelting.c \
../src/streams.c \
../src/tileentity.c \
../src/tools.c \
../src/util.c \
../src/work.c \
../src/world.c \
../src/worldmanager.c \
../src/hashmap.c \
../src/profile.c \
../src/anticheat.c \
../src/plugin.c \
../src/ai.c \
../src/perlin.c \
../src/xstring.c \
../src/prqueue.c
OBJS += \
./src/accept.o \
./src/block.o \
./src/collection.o \
./src/command.o \
./src/config.o \
./src/crafting.o \
./src/entity.o \
./src/game.o \
./src/inventory.o \
./src/item.o \
./src/json.o \
./src/log.o \
./src/main.o \
./src/nbt.o \
./src/network.o \
./src/packet.o \
./src/player.o \
./src/queue.o \
./src/smelting.o \
./src/streams.o \
./src/tileentity.o \
./src/tools.o \
./src/util.o \
./src/work.o \
./src/world.o \
./src/worldmanager.o \
./src/hashmap.o \
./src/profile.o \
./src/anticheat.o \
./src/plugin.o \
./src/ai.o \
./src/perlin.o \
./src/xstring.o \
./src/prqueue.o
C_DEPS += \
./src/accept.d \
./src/block.d \
./src/collection.d \
./src/command.d \
./src/config.d \
./src/crafting.d \
./src/entity.d \
./src/game.d \
./src/inventory.d \
./src/item.d \
./src/json.d \
./src/log.d \
./src/main.d \
./src/nbt.d \
./src/network.d \
./src/packet.d \
./src/player.d \
./src/queue.d \
./src/smelting.d \
./src/streams.d \
./src/tileentity.d \
./src/tools.d \
./src/util.d \
./src/work.d \
./src/world.d \
./src/worldmanager.d \
./src/hashmap.d \
./src/profile.d \
./src/anticheat.d \
./src/plugin.d \
./src/ai.d \
./src/perlin.d \
./src/xstring.d \
./src/prqueue.d
# Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.c
@echo 'Building file: $<'
@echo 'Invoking: GCC C Compiler'
$(CC) $(CFLAGS) -std=gnu11 -I${JAVA_HOME}/include/ -I${JAVA_HOME}/include/linux/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

View File

@ -1,8 +0,0 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
# Each subdirectory must supply rules for building sources it contributes

View File

@ -1,13 +0,0 @@
#ifndef BASIN_BASIN_H_
#define BASIN_BASIN_H_
#include "world.h"
#include "item.h"
#include "game.h"
#include "block.h"
#include "entity.h"
#include "globals.h"
#include "player.h"
#include "inventory.h"
#endif //BASIN_BASIN_H_

View File

@ -1,103 +0,0 @@
/*
* queue.c
*
* Created on: Nov 19, 2015
* Author: root
*/
#include "collection.h"
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include "xstring.h"
#include "util.h"
struct collection* new_collection(size_t capacity, uint8_t mc) {
struct collection* coll = xmalloc(sizeof(struct collection));
if (capacity < 1) capacity = 1;
coll->capacity = capacity;
coll->data = xcalloc((capacity == 0 ? 1 : capacity) * sizeof(void*));
coll->size = 0;
coll->count = 0;
coll->mc = mc;
if (mc && pthread_rwlock_init(&coll->data_mutex, NULL)) {
xfree(coll->data);
coll->data = NULL;
xfree(coll);
return NULL;
}
return coll;
}
int del_collection(struct collection* coll) {
if (coll == NULL || coll->data == NULL) return -1;
if (coll->mc && pthread_rwlock_destroy(&coll->data_mutex)) return -1;
xfree(coll->data);
coll->data = NULL;
xfree(coll);
return 0;
}
int add_collection(struct collection* coll, void* data) {
if (coll->mc) pthread_rwlock_wrlock(&coll->data_mutex);
if (coll->size >= coll->capacity) {
for (int i = 0; i < coll->capacity; i++) {
if (coll->data[i] == NULL) {
coll->count++;
if (i >= coll->size) coll->size++;
coll->data[i] = data;
if (coll->mc) pthread_rwlock_unlock(&coll->data_mutex);
return 0;
}
}
if (coll->count >= coll->capacity) {
size_t oc = coll->capacity;
coll->capacity += 1024 / sizeof(void*);
coll->data = xrealloc(coll->data, coll->capacity * sizeof(void*));
memset(coll->data + oc, 0, 1024);
}
}
coll->data[coll->size++] = data;
coll->count++;
if (coll->mc) pthread_rwlock_unlock(&coll->data_mutex);
return 0;
}
int rem_collection(struct collection* coll, void* data) {
if (coll->mc) pthread_rwlock_wrlock(&coll->data_mutex);
for (int i = 0; i < coll->size; i++) {
if (coll->data[i] == data) {
coll->data[i] = NULL;
coll->count--;
if (i == coll->size - 1) coll->size--;
if (coll->mc) pthread_rwlock_unlock(&coll->data_mutex);
return 0;
}
}
if (coll->mc) pthread_rwlock_unlock(&coll->data_mutex);
return -1;
}
int contains_collection(struct collection* coll, void* data) {
if (coll->mc) pthread_rwlock_rdlock(&coll->data_mutex);
for (int i = 0; i < coll->size; i++) {
if (coll->data[i] == data) {
if (coll->mc) pthread_rwlock_unlock(&coll->data_mutex);
return 1;
}
}
if (coll->mc) pthread_rwlock_unlock(&coll->data_mutex);
return 0;
}
void ensure_collection(struct collection* coll, size_t size) {
if (coll->mc) pthread_rwlock_rdlock(&coll->data_mutex);
if (coll->count + size >= coll->capacity) {
size_t oc = coll->capacity;
coll->capacity = coll->count + size;
coll->data = xrealloc(coll->data, coll->capacity * sizeof(void*));
memset(coll->data + oc, 0, (coll->capacity - oc) * sizeof(void*));
}
if (coll->mc) pthread_rwlock_unlock(&coll->data_mutex);
}

View File

@ -1,36 +0,0 @@
/*
* queue.h
*
* Created on: Nov 19, 2015
* Author: root
*/
#ifndef COLLECTION_H_
#define COLLECTION_H_
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
struct collection {
size_t size;
size_t count;
size_t capacity;
void** data;
int8_t mc;
pthread_rwlock_t data_mutex;
};
struct collection* new_collection(size_t capacity, uint8_t mc);
int del_collection(struct collection* coll);
int add_collection(struct collection* coll, void* data);
int rem_collection(struct collection* coll, void* data);
int contains_collection(struct collection* coll, void* data);
void ensure_collection(struct collection* coll, size_t size);
#endif /* COLLECTION_H_ */

View File

@ -1,173 +0,0 @@
/*
* config.c
*
* Created on: Nov 17, 2015
* Author: root
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "util.h"
#include "config.h"
#include "streams.h"
#include "xstring.h"
struct config* loadConfig(const char* file) {
if (file == NULL) {
errno = EBADF;
return NULL;
}
if (access(file, F_OK)) {
errno = EINVAL;
return NULL;
}
if (access(file, R_OK)) {
errno = EPERM;
return NULL;
}
int fd = open(file, O_RDONLY);
if (fd < 0) return NULL;
struct config* ret = xmalloc(sizeof(struct config));
ret->node_count = 0;
ret->nodes = NULL;
char line[1024];
int l = 0;
struct cnode* cat = NULL;
while (1) {
l = readLine(fd, line, 1024);
if (l < 0) break;
char* wl = trim(line);
if (wl[0] == 0) continue;
char* comment = strchr(line, '#');
if (comment != NULL) {
comment[0] = 0;
wl = trim(line);
if (wl[0] == 0) continue;
}
l = strlen(wl);
if (l > 5 && wl[0] == '[' && wl[l - 1] == ']') {
wl[--l] = 0;
wl++;
char* id = strchr(wl, ' ');
if (id != NULL) {
id[0] = 0;
id++;
id = trim(id);
}
wl = trim(wl);
cat = xmalloc(sizeof(struct cnode));
if (ret->node_count == 0) {
ret->nodes = xmalloc(sizeof(struct cnode*));
ret->node_count = 1;
} else {
ret->node_count++;
ret->nodes = xrealloc(ret->nodes, sizeof(struct cnode*) * ret->node_count);
}
ret->nodes[ret->node_count - 1] = cat;
cat->keys = NULL;
cat->values = NULL;
cat->entries = 0;
if (streq_nocase(wl, "server")) {
cat->cat = CAT_SERVER;
} else if (streq_nocase(wl, "daemon")) {
cat->cat = CAT_DAEMON;
} else {
cat->cat = CAT_UNKNOWN;
}
if (id == NULL) {
cat->id = NULL;
} else {
int idl = strlen(id) + 1;
cat->id = xmalloc(idl);
memcpy(cat->id, id, idl);
}
} else {
char* value = strchr(wl, '=');
if (value == NULL) continue;
value[0] = 0;
value++;
value = trim(value);
wl = trim(wl);
int wll = strlen(wl);
int vl = strlen(value);
if (cat->entries == 0) {
cat->keys = xmalloc(sizeof(char*));
cat->values = xmalloc(sizeof(char*));
cat->entries = 1;
} else {
cat->keys = xrealloc(cat->keys, ++cat->entries * sizeof(char*));
cat->values = xrealloc(cat->values, cat->entries * sizeof(char*));
}
cat->keys[cat->entries - 1] = xmalloc(wll + 1);
cat->values[cat->entries - 1] = xmalloc(vl + 1);
memcpy(cat->keys[cat->entries - 1], wl, wll + 1);
memcpy(cat->values[cat->entries - 1], value, vl + 1);
}
}
close(fd);
return ret;
}
const char* getConfigValue(const struct cnode* cat, const char* name) {
if (cat == NULL || name == NULL || cat->entries == 0) return NULL;
for (int i = 0; i < cat->entries; i++) {
if (streq_nocase(cat->keys[i], name)) {
return cat->values[i];
}
}
return NULL;
}
int hasConfigKey(const struct cnode* cat, const char* name) {
if (cat == NULL || name == NULL || cat->entries == 0) return 0;
for (int i = 0; i < cat->entries; i++) {
if (streq_nocase(cat->keys[i], name)) {
return 1;
}
}
return 0;
}
struct cnode* getCatByID(const struct config* cfg, const char* id) {
if (cfg == NULL || id == NULL || cfg->node_count == 0) return NULL;
for (int i = 0; i < cfg->node_count; i++) {
if (streq_nocase(cfg->nodes[i]->id, id)) {
return cfg->nodes[i];
}
}
return NULL;
}
struct cnode** getCatsByCat(const struct config* cfg, int cat, int* count) {
if (cfg == NULL || cat < CAT_UNKNOWN || cfg->node_count == 0) return NULL;
if (cat == CAT_UNKNOWN) return cfg->nodes;
int rs = 0;
struct cnode** ret = NULL;
for (int i = 0; i < cfg->node_count; i++) {
if (cfg->nodes[i]->cat == cat) {
if (rs == 0) {
rs = 1;
ret = xmalloc(sizeof(struct cnode**));
} else {
rs++;
ret = xrealloc(ret, sizeof(struct cnode**) * rs);
}
ret[rs - 1] = cfg->nodes[i];
}
}
*count = rs;
return ret;
}
struct cnode* getUniqueByCat(const struct config* cfg, int cat) {
if (cfg == NULL || cat <= CAT_UNKNOWN || cfg->node_count == 0) return NULL;
for (int i = 0; i < cfg->node_count; i++) {
if (cfg->nodes[i]->cat == cat && cfg->nodes[i]->id == NULL) {
return cfg->nodes[i];
}
}
return NULL;
}

View File

@ -1,40 +0,0 @@
/*
* config.h
*
* Created on: Nov 17, 2015
* Author: root
*/
#ifndef CONFIG_H_
#define CONFIG_H_
#define CAT_UNKNOWN -1
#define CAT_SERVER 0
#define CAT_DAEMON 1
struct cnode {
int cat;
char* id;
int entries;
char** keys;
char** values;
};
struct config {
int node_count;
struct cnode** nodes;
};
struct config* loadConfig(const char* file);
const char* getConfigValue(const struct cnode* cat, const char* name);
int hasConfigKey(const struct cnode* cat, const char* name);
struct cnode* getCatByID(const struct config* cfg, const char* name);
struct cnode** getCatsByCat(const struct config* cfg, int cat, int* count);
struct cnode* getUniqueByCat(const struct config* cfg, int cat);
#endif /* CONFIG_H_ */

View File

@ -1,123 +0,0 @@
/*
* hashmap.c
*
* Created on: Nov 19, 2015
* Author: root
*/
#include "hashmap.h"
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include "xstring.h"
#include "util.h"
#include <math.h>
#include <sys/types.h>
struct hashmap* new_hashmap(uint8_t bucket_count_bytes, uint8_t mc) {
if (bucket_count_bytes > 2 || bucket_count_bytes < 1) return NULL;
struct hashmap* map = xmalloc(sizeof(struct hashmap));
map->bucket_count = (size_t) pow(2, 8 * bucket_count_bytes);
map->buckets = xcalloc(map->bucket_count * sizeof(struct hashmap_entry*));
map->entry_count = 0;
map->mc = mc;
if (mc && pthread_rwlock_init(&map->data_mutex, NULL)) {
xfree(map->buckets);
map->buckets = NULL;
xfree(map);
return NULL;
}
return map;
}
int del_hashmap(struct hashmap* map) {
if (map == NULL || map->buckets == NULL) return -1;
if (map->mc && pthread_rwlock_destroy(&map->data_mutex)) return -1;
for (size_t i = 0; i < map->bucket_count; i++) {
struct hashmap_entry* head = map->buckets[i];
while (1) {
if (head == NULL) break;
struct hashmap_entry* next = head->next;
xfree(head);
head = next;
}
}
xfree(map->buckets);
map->buckets = NULL;
xfree(map);
return 0;
}
void put_hashmap(struct hashmap* map, uint64_t key, void* value) {
if (map->mc) pthread_rwlock_wrlock(&map->data_mutex);
uint64_t hkey = key;
uint8_t* hashi = (uint8_t*) &hkey;
for (int i = 1; i < sizeof(uint64_t); i++) {
hashi[0] ^= hashi[i];
if (i > 1 && map->bucket_count == sizeof(uint16_t)) hashi[1] ^= hashi[i];
}
if (map->bucket_count == 65536) hashi[1] ^= hashi[0];
uint16_t fhash = hashi[0];
//printf("%i\n", fhash);
if (map->bucket_count == 65536) fhash |= (hashi[1] << 8);
struct hashmap_entry* prior = map->buckets[fhash];
struct hashmap_entry* entry = map->buckets[fhash];
while (entry != NULL && entry->key != key) {
prior = entry;
entry = entry->next;
}
if (entry == NULL) {
if (value != NULL) {
map->entry_count++;
entry = xmalloc(sizeof(struct hashmap_entry));
entry->key = key;
entry->value = value;
entry->next = NULL;
if (prior != NULL && prior != entry) {
prior->next = entry;
} else {
map->buckets[fhash] = entry;
}
}
} else {
if (value == NULL) {
if (prior != entry && prior != NULL) {
prior->next = entry->next;
} else {
map->buckets[fhash] = entry->next;
}
xfree(entry);
map->entry_count--;
} else {
entry->value = value;
}
}
if (map->mc) pthread_rwlock_unlock(&map->data_mutex);
}
void* get_hashmap(struct hashmap* map, uint64_t key) {
if (map->mc) pthread_rwlock_rdlock(&map->data_mutex);
uint64_t hkey = key;
uint8_t* hashi = (uint8_t*) &hkey;
for (int i = 1; i < sizeof(uint64_t); i++) {
hashi[0] ^= hashi[i];
if (i > 1 && map->bucket_count == 65536) hashi[1] ^= hashi[i];
}
if (map->bucket_count == 65536) hashi[1] ^= hashi[0];
uint16_t fhash = hashi[0];
if (map->bucket_count == 65536) fhash |= (hashi[1] << 8);
struct hashmap_entry* entry = map->buckets[fhash];
while (entry != NULL && entry->key != key) {
entry = entry->next;
}
void* value = NULL;
if (entry != NULL) {
value = entry->value;
}
if (map->mc) pthread_rwlock_unlock(&map->data_mutex);
return value;
}
int contains_hashmap(struct hashmap* map, uint64_t key) {
return get_hashmap(map, key) != NULL;
}

View File

@ -1,46 +0,0 @@
/*
* hashmap.h
*
* Created on: Nov 19, 2015
* Author: root
*/
#ifndef HASHMAP_H_
#define HASHMAP_H_
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
struct hashmap {
struct hashmap_entry** buckets;
size_t bucket_count;
size_t entry_count;
uint8_t mc;
pthread_rwlock_t data_mutex;
};
struct hashmap_entry {
uint64_t key;
void* value;
struct hashmap_entry* next;
};
struct hashmap* new_hashmap(uint8_t bucket_count_bytes, uint8_t mc); // bucket_count_bytes must be 1 or 2.
int del_hashmap(struct hashmap* map);
void put_hashmap(struct hashmap* map, uint64_t key, void* value);
void* get_hashmap(struct hashmap* map, uint64_t key);
int rem_hashmap(struct hashmap* map, uint64_t data);
int contains_hashmap(struct hashmap* map, uint64_t data);
#define BEGIN_HASHMAP_ITERATION(hashmap) {if (hashmap->mc) pthread_rwlock_rdlock(&hashmap->data_mutex); for (size_t _hmi = 0; _hmi < hashmap->bucket_count; _hmi++) {struct hashmap_entry* he = hashmap->buckets[_hmi];int _s = 1;struct hashmap_entry* nhe = NULL;while (he != NULL) {if(!_s)he = nhe;else _s=0;if(he == NULL)break;nhe = he->next;uint64_t key = he->key;void* value = he->value;
#define END_HASHMAP_ITERATION(hashmap) }} if (hashmap->mc) pthread_rwlock_unlock(&hashmap->data_mutex);}
#define BREAK_HASHMAP_ITERATION(hashmap) if (hashmap->mc) pthread_rwlock_unlock(&hashmap->data_mutex);
#endif /* HASHMAP_H_ */

View File

@ -1,235 +0,0 @@
/*
* json.c
*
* Created on: May 20, 2016
* Author: root
*/
#include "xstring.h"
#include "json.h"
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "util.h"
char* __readJSONString(char* json, size_t* i) {
int cs = 0;
size_t sl = strlen(json);
char* ret = NULL;
size_t rs = 0;
for (; *i < sl; (*i)++) {
if (cs == 0) {
if (json[*i] == '\"') cs = 1;
} else if (cs == 1) {
if (json[*i] == '\"') {
errno = 0;
if (ret == NULL) {
ret = xmalloc(1);
rs = 0;
}
ret[rs] = 0;
return ret;
} else if (json[*i] == '\\') {
(*i)++;
if (*i >= sl) continue;
if (ret == NULL) {
ret = xmalloc(2);
rs = 0;
} else ret = xrealloc(ret, rs + 2);
if (json[*i] == '\"') ret[rs++] = '\"';
else if (json[*i] == '\\') ret[rs++] = '\\';
else if (json[*i] == '/') ret[rs++] = '/';
else if (json[*i] == '\b') ret[rs++] = '\b';
else if (json[*i] == '\f') ret[rs++] = '\f';
else if (json[*i] == '\n') ret[rs++] = '\n';
else if (json[*i] == '\r') ret[rs++] = '\r';
else if (json[*i] == '\t') ret[rs++] = '\t';
else if (json[*i] == 'u') {
(*i)--;
goto uce;
}
ret[rs] = 0;
continue;
uce: ;
}
if (ret == NULL) {
ret = xmalloc(2);
rs = 0;
} else ret = xrealloc(ret, rs + 2);
ret[rs++] = json[*i];
ret[rs] = 0;
}
}
if (ret == NULL) errno = EINVAL;
else ret[rs] = 0;
return ret;
}
int __recurJSON(struct json_object* cur, size_t* i, char* json);
int __readJSONValue(char* json, size_t* i, struct json_object* into) {
size_t sl = strlen(json);
into->children = NULL;
into->child_count = 0;
into->data.string = NULL;
for (; *i < sl; (*i)++) {
if (json[*i] == '\"') {
into->type = JSON_STRING;
into->data.string = __readJSONString(json, i);
} else if (json[*i] == '-' || (json[*i] >= '0' && json[*i] <= '9')) {
into->type = JSON_NUMBER;
char* tp = NULL;
into->data.number = strtod(json + *i, &tp);
*i = tp - json - 1;
} else if (json[*i] == '{') {
into->type = JSON_OBJECT;
if (__recurJSON(into, i, json)) return -1;
} else if (json[*i] == '[') {
into->type = JSON_ARRAY;
int cs = 0;
size_t sl = strlen(json);
for (; *i < sl; (*i)++) {
if (cs == 0) {
if (json[*i] == '[') {
cs = 1;
}
} else if (cs == 1) {
struct json_object* sj = xmalloc(sizeof(struct json_object));
sj->name = NULL;
if (__readJSONValue(json, i, sj)) {
xfree(sj);
return -1;
}
if (sj->type == JSON_EOA) {
xfree(sj);
break;
} else {
if (into->children == NULL) {
into->child_count = 0;
into->children = xmalloc(sizeof(struct json_object*));
} else {
into->children = xrealloc(into->children, sizeof(struct json_object*) * (into->child_count + 1));
}
into->children[into->child_count++] = sj;
cs = 2;
}
} else if (cs == 2) {
if (json[*i] == ',') cs = 1;
else if (json[*i] == ']') break;
}
}
} else if (startsWith_nocase(json + *i, "true")) {
(*i) += 3;
into->type = JSON_TRUE;
} else if (startsWith_nocase(json + *i, "false")) {
(*i) += 4;
into->type = JSON_FALSE;
} else if (startsWith_nocase(json + *i, "null")) {
(*i) += 3;
into->type = JSON_NULL;
} else if (json[*i] == ']') {
into->type = JSON_EOA;
} else continue;
break;
}
return 0;
}
int __recurJSON(struct json_object* cur, size_t* i, char* json) {
size_t sl = strlen(json);
int cs = 0;
struct json_object* nchild = NULL;
char ck = cur->type == JSON_OBJECT ? '}' : ']';
for (; *i < sl; (*i)++) {
if (cs == 0) {
if (json[*i] == '{') cs = 1;
} else if (cs == 1) {
if (json[*i] == ck) break;
else if (json[*i] == '\"') {
nchild = xmalloc(sizeof(struct json_object));
memset(nchild, 0, sizeof(struct json_object));
nchild->name = __readJSONString(json, i);
if (nchild->name == NULL) {
xfree(nchild);
errno = EINVAL;
return -1;
}
cs = 2;
}
} else if (cs == 2) {
if (json[*i] == ':') cs = 3;
} else if (cs == 3) {
if (__readJSONValue(json, i, nchild)) {
if (nchild->name != NULL) xfree(nchild->name);
xfree(nchild);
return -1;
}
if (cur->children == NULL) {
cur->child_count = 0;
cur->children = xmalloc(sizeof(struct json_object*));
} else {
cur->children = xrealloc(cur->children, sizeof(struct json_object*) * (cur->child_count + 1));
}
cur->children[cur->child_count++] = nchild;
nchild = NULL;
cs = 4;
} else if (cs == 4) {
if (json[*i] == ',') cs = 1;
else if (json[*i] == ck) break;
}
}
if (nchild != NULL) {
if (nchild->name != NULL) xfree(nchild->name);
xfree(nchild);
}
return 0;
}
ssize_t parseJSON(struct json_object* root, char* json) {
if (strlen(json) < 2 || (json[0] != '{' && json[0] != '[')) {
memset(root, 0, sizeof(struct json_object));
errno = EINVAL;
return -1;
}
root->name = NULL;
root->type = json[0] == '{' ? JSON_OBJECT : JSON_ARRAY;
root->children = NULL;
root->child_count = 0;
root->data.string = NULL;
size_t it = 0;
__recurJSON(root, &it, json);
return it;
}
void freeJSON(struct json_object* root) {
if (root->name != NULL) {
xfree(root->name);
root->name = NULL;
}
if (root->type == JSON_STRING && root->data.string != NULL) {
xfree(root->data.string);
root->data.string = NULL;
} else if (root->type == JSON_OBJECT && root->children != NULL) {
for (size_t i = 0; i < root->child_count; i++) {
if (root->children[i] != NULL) {
freeJSON(root->children[i]);
xfree(root->children[i]);
root->children[i] = NULL;
}
}
if (root->children != NULL) {
xfree(root->children);
root->children = NULL;
}
}
}
struct json_object* getJSONValue(struct json_object* parent, char* name) {
if (parent->children == NULL) return NULL;
for (size_t i = 0; i < parent->child_count; i++) {
if (parent->children[i] != NULL && streq_nocase(parent->children[i]->name, name)) {
return parent->children[i];
}
}
return NULL;
}

View File

@ -1,43 +0,0 @@
/*
* json.h
*
* Created on: May 20, 2016
* Author: root
*/
#ifndef JSON_H_
#define JSON_H_
#include <stdint.h>
#include <stdlib.h>
#define JSON_STRING 0
#define JSON_NUMBER 1
#define JSON_OBJECT 2
#define JSON_ARRAY 3
#define JSON_TRUE 4
#define JSON_FALSE 5
#define JSON_NULL 6
#define JSON_EOA 7
union json_data {
char* string;
double number;
struct json_object* object;
};
struct json_object {
char* name;
uint8_t type;
union json_data data;
struct json_object** children;
size_t child_count;
};
ssize_t parseJSON(struct json_object* root, char* json);
void freeJSON(struct json_object* root);
struct json_object* getJSONValue(struct json_object* parent, char* name);
#endif /* JSON_H_ */

View File

@ -1,97 +0,0 @@
/*
* log.c
*
* Created on: Nov 22, 2015
* Author: root
*/
#include "log.h"
#include <stdio.h>
#include <sys/time.h>
#include "xstring.h"
#include <pthread.h>
#include <errno.h>
#include <stdarg.h>
void acclog(struct logsess* logsess, char* template, ...) {
if (!logsess->pi) {
if (pthread_mutex_init(&logsess->lmutex, NULL) == -1) {
printf("Failed to create logging mutex! %s\n", strerror(errno));
logsess->pi = -1;
} else {
logsess->pi = 1;
}
}
struct timeval tv;
gettimeofday(&tv, NULL);
if (logsess->pi == 1) pthread_mutex_lock(&logsess->lmutex);
struct tm *ctm = localtime(&tv.tv_sec);
char ct[32]; // the above uses a static buffer, so problems could ensue, but it would be the same time being overwritten...
strftime(ct, 31, "%Y-%m-%d %H:%M:%S", ctm);
va_list arg;
va_start(arg, template);
size_t ctl = strlen(ct);
size_t tl = strlen(template);
char tp[ctl + 5 + tl];
tp[0] = '[';
memcpy(tp + 1, ct, ctl);
tp[1 + ctl] = ']';
tp[2 + ctl] = ' ';
memcpy(tp + 3 + ctl, template, tl);
tp[3 + ctl + tl] = '\n';
tp[4 + ctl + tl] = 0;
if (vfprintf(stdout, tp, arg) < 0) {
errlog(logsess, "Failed writing to stdout!");
}
va_end(arg);
va_start(arg, template);
if (logsess->access_fd != NULL) {
if (vfprintf(logsess->access_fd, tp, arg) < 0) {
errlog(logsess, "Failed writing to accesslog!");
}
}
va_end(arg);
if (logsess->pi == 1) pthread_mutex_unlock(&logsess->lmutex);
}
void errlog(struct logsess* logsess, char* template, ...) {
if (!logsess->pi) {
if (pthread_mutex_init(&logsess->lmutex, NULL) == -1) {
printf("Failed to create logging mutex! %s\n", strerror(errno));
logsess->pi = -1;
} else {
logsess->pi = 1;
}
}
struct timeval tv;
gettimeofday(&tv, NULL);
if (logsess->pi == 1) pthread_mutex_lock(&logsess->lmutex);
struct tm *ctm = localtime(&tv.tv_sec);
char ct[32]; // the above uses a static buffer, so problems could ensue, but it would be the same time being overwritten...
strftime(ct, 31, "%Y-%m-%d %H:%M:%S", ctm);
va_list arg;
va_start(arg, template);
size_t ctl = strlen(ct);
size_t
tl = strlen(template);
char tp[ctl + 5 + tl];
tp[0] = '[';
memcpy(tp + 1, ct, ctl);
tp[1 + ctl] = ']';
tp[2 + ctl] = ' ';
memcpy(tp + 3 + ctl, template, tl);
tp[3 + ctl + tl] = '\n';
tp[4 + ctl + tl] = 0;
if (vfprintf(stdout, tp, arg) < 0) {
//TODO: we can't write to stdout, nothing we can do!
}
va_end(arg);
va_start(arg, template);
if (logsess->error_fd != NULL) {
if (vfprintf(logsess->error_fd, tp, arg) < 0) {
//its in the console
}
}
va_end(arg);
if (logsess->pi == 1) pthread_mutex_unlock(&logsess->lmutex);
}

View File

@ -1,26 +0,0 @@
/*
* log.h
*
* Created on: Nov 22, 2015
* Author: root
*/
#ifndef LOG_H_
#define LOG_H_
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
struct logsess {
pthread_mutex_t lmutex;
int pi;
FILE* access_fd;
FILE* error_fd;
};
void acclog(struct logsess* logsess, char* template, ...);
void errlog(struct logsess* logsess, char* template, ...);
#endif /* LOG_H_ */

View File

@ -1,99 +0,0 @@
/*
* profile.c
*
* Created on: Dec 30, 2016
* Author: root
*/
#include "xstring.h"
#include <time.h>
#include "util.h"
#include <stdio.h>
#include "hashmap.h"
#define ENABLE_PROFILER
//#define ENABLE_PROFILER_MT
struct hashmap* psec;
struct profiler_section {
char* name;
double ms_spent;
double sectionLastStart;
double creation;
double maxTime;
};
void beginProfilerSection(char* name) {
#ifdef ENABLE_PROFILER
if (psec == NULL) {
#ifdef ENABLE_PROFILER_MT
psec = new_hashmap(1, 1);
#else
psec = new_hashmap(1, 0);
#endif
}
struct profiler_section* ps = get_hashmap(psec, (uint64_t) name);
if (ps != NULL) {
if (ps->sectionLastStart > 0.) return;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
ps->sectionLastStart = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
} else {
struct profiler_section* ps = xmalloc(sizeof(struct profiler_section));
ps->name = name;
ps->ms_spent = 0.;
ps->maxTime = 0.;
put_hashmap(psec, (uint64_t) name, ps);
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
ps->sectionLastStart = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
ps->creation = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
}
#endif
}
void endProfilerSection(char* name) {
#ifdef ENABLE_PROFILER
if (psec == NULL) return;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
double now = ((double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.);
struct profiler_section* ps = get_hashmap(psec, (uint64_t) name);
if (ps == NULL) return;
if (ps->sectionLastStart < 0.) return;
double msa = now - ps->sectionLastStart;
ps->ms_spent += msa;
ps->sectionLastStart = -1.;
if (ps->maxTime < msa) {
ps->maxTime = msa;
}
#endif
}
void printProfiler() {
#ifdef ENABLE_PROFILER
if (psec == NULL) return;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
double now = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
BEGIN_HASHMAP_ITERATION (psec)
struct profiler_section* ps = value;
if (ps->sectionLastStart > 0.) {
ps->ms_spent += now - ps->sectionLastStart;
ps->sectionLastStart = -1.;
}
printf("%s: %f ms - %f ms/tick - %f maximum\n", ps->name, ps->ms_spent, ps->ms_spent / (now - ps->creation) * 50., ps->maxTime);
END_HASHMAP_ITERATION (psec)
#endif
}
void clearProfiler() {
#ifdef ENABLE_PROFILER
BEGIN_HASHMAP_ITERATION (psec)
struct profiler_section* ps = value;
ps->ms_spent = 0.;
ps->maxTime = 0.;
END_HASHMAP_ITERATION (psec)
#endif
}

View File

@ -1,185 +0,0 @@
/*
* queue.c
*
* Created on: Nov 19, 2015
* Author: root
*/
#include "queue.h"
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include "xstring.h"
#include <stdlib.h>
#include "util.h"
struct queue* new_queue(size_t capacity, int mt) {
struct queue* queue = xmalloc(sizeof(struct queue));
queue->capacity = capacity;
queue->data = xmalloc((capacity == 0 ? 1 : capacity) * sizeof(void*));
queue->rc = capacity == 0 ? 1 : 0;
queue->start = 0;
queue->end = 0;
queue->size = 0;
queue->mt = mt;
if (mt) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
if (pthread_mutex_init(&queue->data_mutex, &attr)) {
xfree(queue->data);
queue->data = NULL;
xfree(queue);
return NULL;
}
if (pthread_cond_init(&queue->out_cond, NULL)) {
xfree(queue->data);
queue->data = NULL;
pthread_mutex_destroy(&queue->data_mutex);
xfree(queue);
return NULL;
}
if (pthread_cond_init(&queue->in_cond, NULL)) {
xfree(queue->data);
queue->data = NULL;
pthread_mutex_destroy(&queue->data_mutex);
pthread_cond_destroy(&queue->out_cond);
xfree(queue);
return NULL;
}
}
return queue;
}
int del_queue(struct queue* queue) {
if (queue == NULL || queue->data == NULL) return -1;
if (queue->mt) {
if (pthread_mutex_destroy(&queue->data_mutex)) return -1;
if (pthread_cond_destroy(&queue->out_cond)) return -1;
if (pthread_cond_destroy(&queue->in_cond)) return -1;
}
xfree(queue->data);
queue->data = NULL;
xfree(queue);
return 0;
}
int add_queue(struct queue* queue, void* data) {
if (queue->mt) pthread_mutex_lock(&queue->data_mutex);
if (queue->size == queue->rc && queue->capacity == 0) {
size_t orc = queue->rc;
queue->rc += 1024 / sizeof(void*);
void** ndata = xmalloc(queue->rc * sizeof(void*));
if (queue->start < queue->end) {
memcpy(ndata, queue->data + queue->start, (queue->end - queue->start) * sizeof(void*));
} else {
memcpy(ndata, queue->data + queue->start, (orc - queue->start) * sizeof(void*));
memcpy(ndata + (orc - queue->start), queue->data, (queue->end) * sizeof(void*));
}
xfree(queue->data);
queue->data = ndata;
queue->start = 0;
queue->end = orc;
} else if (queue->capacity == 0) {
} else {
while (queue->size == queue->capacity) {
if (!queue->mt) return 1;
pthread_cond_wait(&queue->in_cond, &queue->data_mutex);
}
}
queue->data[queue->end++] = data;
size_t rp = queue->capacity > 0 ? queue->capacity : queue->rc;
if (queue->end >= rp && queue->capacity == 0) {
if (queue->end - rp == queue->start) {
size_t orc = queue->rc;
queue->rc += 1024 / sizeof(void*);
void** ndata = xmalloc(queue->rc * sizeof(void*));
if (queue->start < queue->end) {
memcpy(ndata, queue->data + queue->start, (queue->end - queue->start) * sizeof(void*));
} else {
memcpy(ndata, queue->data + queue->start, (orc - queue->start) * sizeof(void*));
memcpy(ndata + (orc - queue->start), queue->data, (queue->end) * sizeof(void*));
}
xfree(queue->data);
queue->data = ndata;
queue->start = 0;
queue->end = orc;
} else queue->end -= rp;
}
queue->size++;
if (queue->mt) {
pthread_mutex_unlock(&queue->data_mutex);
pthread_cond_signal(&queue->out_cond);
}
return 0;
}
void* pop_queue(struct queue* queue) {
if (queue->mt) {
pthread_mutex_lock(&queue->data_mutex);
while (queue->size == 0) {
pthread_cond_wait(&queue->out_cond, &queue->data_mutex);
}
} else if (queue->size == 0) {
return NULL;
}
void* data = queue->data[queue->start++];
size_t rp = queue->capacity > 0 ? queue->capacity : queue->rc;
if (queue->start >= rp) {
queue->start -= rp;
}
queue->size--;
if (queue->mt) {
pthread_mutex_unlock(&queue->data_mutex);
pthread_cond_signal(&queue->in_cond);
}
return data;
}
void* pop_nowait_queue(struct queue* queue) {
if (queue->mt) {
pthread_mutex_lock(&queue->data_mutex);
}
if (queue->size <= 0) {
if (queue->mt) pthread_mutex_unlock(&queue->data_mutex);
return NULL;
}
void* data = queue->data[queue->start++];
size_t rp = queue->capacity > 0 ? queue->capacity : queue->rc;
if (queue->start >= rp) {
queue->start -= rp;
}
queue->size--;
if (queue->mt) {
pthread_mutex_unlock(&queue->data_mutex);
pthread_cond_signal(&queue->in_cond);
}
return data;
}
void* timedpop_queue(struct queue* queue, struct timespec* abstime) {
if (queue->mt) {
pthread_mutex_lock(&queue->data_mutex);
while (queue->size == 0) {
int x = pthread_cond_timedwait(&queue->out_cond, &queue->data_mutex, abstime);
if (x) {
pthread_mutex_unlock(&queue->data_mutex);
errno = x;
return NULL;
}
}
} else if (queue->size == 0) {
return NULL;
}
void* data = queue->data[queue->start++];
size_t rp = queue->capacity > 0 ? queue->capacity : queue->rc;
if (queue->start >= rp) {
queue->start -= rp;
}
queue->size--;
if (queue->mt) {
pthread_mutex_unlock(&queue->data_mutex);
pthread_cond_signal(&queue->in_cond);
}
return data;
}

View File

@ -1,38 +0,0 @@
/*
* queue.h
*
* Created on: Nov 19, 2015
* Author: root
*/
#ifndef QUEUE_H_
#define QUEUE_H_
#include <pthread.h>
struct queue {
size_t size;
size_t capacity;
size_t start;
size_t end;
size_t rc;
void** data;
pthread_mutex_t data_mutex;
pthread_cond_t in_cond;
pthread_cond_t out_cond;
int mt;
};
struct queue* new_queue(size_t capacity, int mt);
int del_queue(struct queue* queue);
int add_queue(struct queue* queue, void* data);
void* pop_queue(struct queue* queue);
void* pop_nowait_queue(struct queue* queue);
void* timedpop_queue(struct queue* queue, struct timespec* abstime);
#endif /* QUEUE_H_ */

View File

@ -1,47 +0,0 @@
/*
* streams.c
*
* Created on: Nov 17, 2015
* Author: root
*/
#include <stdio.h>
#include <unistd.h>
ssize_t readLine(int fd, char* line, size_t len) {
if (len >= 1) line[0] = 0;
char b = 0;
int s = 0;
int i = 0;
do {
s = read(fd, &b, 1);
if ((s == 0 && i == 0) || s < 0) {
return -1;
}
if (s == 0) {
break;
}
if (s > 0 && b != 13 && b != 10) {
line[i++] = b;
}
} while (b > -1 && s > 0 && b != 10 && i < len - 1);
line[i] = 0;
return i;
}
ssize_t writeLine(int fd, char* line, size_t len) {
static char nl[2] = { 0x0A, 0x0D };
int i = 0;
while (i < len) {
int x = write(fd, line + i, len - i);
if (x < 0) return -1;
i += x;
}
int i2 = 0;
while (i2 < 2) {
int y = write(fd, nl + i2, 2 - i2);
if (y < 0) return -1;
i2 += y;
}
return i;
}

View File

@ -1,15 +0,0 @@
/*
* streams.h
*
* Created on: Nov 17, 2015
* Author: root
*/
#ifndef STREAMS_H_
#define STREAMS_H_
size_t readLine(int fd, char* line, size_t len);
size_t writeLine(int fd, char* line, size_t len);
#endif /* STREAMS_H_ */

View File

@ -1,94 +0,0 @@
/*
* craft.c
*
* Created on: Dec 26, 2016
* Author: root
*/
#include "tools.h"
#include "network.h"
#include "block.h"
#include "item.h"
#include <fcntl.h>
#include "xstring.h"
#include <errno.h>
#include "util.h"
#include "json.h"
#include "player.h"
#include <unistd.h>
struct collection* tool_infos;
struct tool_info* getToolInfo(char* name) {
for (size_t i = 0; i < tool_infos->size; i++) {
struct tool_info* ti = (struct tool_info*) tool_infos->data[i];
if (streq_nocase(ti->name, name)) return ti;
}
return NULL;
}
int isToolProficient(struct tool_info* ti, uint8_t harvest_level, block blk) {
if (harvest_level < 0 || ti == NULL) return 0;
if (harvest_level > ti->proficient_size) harvest_level = ti->proficient_size - 1;
for (size_t x = 0; x <= harvest_level; x++) {
for (size_t z = 0; z < ti->proficient_hl[x].proficient_size; z++) {
struct tool_proficiency* tp = (struct tool_proficiency*) &ti->proficient_hl[x];
if (tp == NULL) continue;
for (size_t i = 0; i < tp->proficient_size; i++) {
if ((tp->proficient[i] >> 4) == (blk >> 4)) return 1;
}
}
}
return 0;
}
void init_tools() {
tool_infos = new_collection(8, 0);
char* jsf = xmalloc(4097);
size_t jsfs = 4096;
size_t jsr = 0;
int fd = open("tools.json", O_RDONLY);
ssize_t r = 0;
while ((r = read(fd, jsf + jsr, jsfs - jsr)) > 0) {
jsr += r;
if (jsfs - jsr < 512) {
jsfs += 4096;
jsf = xrealloc(jsf, jsfs + 1);
}
}
jsf[jsr] = 0;
if (r < 0) {
printf("Error reading tool data: %s\n", strerror(errno));
}
close(fd);
struct json_object json;
parseJSON(&json, jsf);
for (size_t i = 0; i < json.child_count; i++) {
struct json_object* ur = json.children[i];
struct tool_info* cr = xcalloc(sizeof(struct tool_info));
cr->name = xstrdup(ur->name, 0);
cr->proficient_hl = xmalloc(sizeof(struct tool_proficiency) * ur->child_count);
cr->proficient_size = ur->child_count;
for (size_t x = 0; x < ur->child_count; x++) {
struct json_object* ir = ur->children[x];
if (ir == NULL || ir->type != JSON_ARRAY) goto cerr;
cr->proficient_hl[x].proficient = xmalloc(sizeof(block) * ir->child_count);
cr->proficient_hl[x].proficient_size = ir->child_count;
for (size_t z = 0; z < ir->child_count; z++) {
struct json_object* er = ir->children[z];
if (er == NULL || er->type != JSON_NUMBER) goto cerr;
cr->proficient_hl[x].proficient[z] = (block) er->data.number;
}
}
add_tool(cr);
continue;
cerr: ;
printf("[WARNING] Error Loading Tool \"%s\"! Skipped.\n", ur->name);
}
freeJSON(&json);
xfree(jsf);
}
void add_tool(struct tool_info* tool) {
add_collection(tool_infos, tool);
}

View File

@ -1,35 +0,0 @@
/*
* tools.h
*
* Created on: Dec 26, 2016
* Author: root
*/
#ifndef TOOLS_H_
#define TOOLS_H_
#include "collection.h"
#include "network.h"
#include "item.h"
#include "world.h"
struct tool_proficiency {
uint16_t* proficient;
size_t proficient_size;
};
struct tool_info {
char* name;
struct tool_proficiency* proficient_hl;
size_t proficient_size;
};
void init_tools();
struct tool_info* getToolInfo(char* name);
int isToolProficient(struct tool_info* ti, uint8_t harvest_level, uint16_t blk);
void add_tool(struct tool_info* tool);
#endif /* TOOLS_H_ */

View File

@ -1,146 +0,0 @@
/*
* util.c
*
* Created on: Nov 17, 2015
* Author: root
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "xstring.h"
#include <linux/limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include "util.h"
#include "hashmap.h"
#include <sys/syscall.h>
#ifndef MEM_LEAK_DEBUG
void* xmalloc(size_t size) {
if (size > 10485760) {
//printf("Big malloc %u!\n", size);
}
void* m = malloc(size);
if (m == NULL) {
printf("Out of Memory! @ malloc size %u\n", size);
exit(1);
}
return m;
}
#endif
#ifndef MEM_LEAK_DEBUG
void xfree(void* ptr) {
free(ptr);
}
#endif
#ifndef MEM_LEAK_DEBUG
void* xcalloc(size_t size) {
if (size > 10485760) {
//printf("Big calloc %u!\n", size);
}
void* m = calloc(1, size);
if (m == NULL) {
printf("Out of Memory! @ calloc size %u\n", size);
exit(1);
}
return m;
}
#endif
#ifndef MEM_LEAK_DEBUG
void* xrealloc(void* ptr, size_t size) {
if (size == 0) {
xfree(ptr);
return NULL;
}
if (ptr == NULL) {
return xmalloc(ptr);
}
if (size > 10485760) {
//printf("Big realloc %u!\n", size);
}
void* m = realloc(ptr, size);
if (m == NULL) {
printf("Out of Memory! @ realloc size %u\n", size);
exit(1);
}
return m;
}
#endif
void* xcopy(const void* ptr, size_t size, size_t expand) {
void* alloc = xmalloc(size + expand);
memcpy(alloc, ptr, size);
return alloc;
}
char* xstrdup(const char* str, size_t expand) {
return str == NULL ? NULL : xcopy(str, strlen(str) + 1, expand);
}
// copies up the maxsize bytes from src to dst, until a null byte is reached.
// does not copy the null byte. returns pointer to byte after the last byte of the dst after copying
char* xstrncat(char* dst, size_t maxsize, char* src) {
const char *maxcdst = dst + maxsize;
char *cdst = dst;
for (char *csrc = src; *csrc && cdst < maxcdst; ++cdst, ++csrc)
*cdst = *csrc;
return cdst;
}
int recur_mkdir(const char* path, mode_t mode) {
char rp[PATH_MAX];
realpath(path, rp);
size_t pl = strlen(rp);
char* pp[16];
int ppi = 0;
for (int i = 0; i < pl; i++) {
if (rp[i] == '/') {
if (ppi == 16) break;
pp[ppi++] = &rp[i] + 1;
rp[i] = 0;
}
}
if (strlen(pp[ppi - 1]) == 0) ppi--;
char vp[pl + 1];
vp[pl] = 0;
vp[0] = 0;
for (int i = 0; i < ppi; i++) {
strcat(vp, "/");
strcat(vp, pp[i]);
int r = mkdir(vp, mode);
if (r == -1 && errno != EEXIST) {
return -1;
}
}
return 0;
}
int memeq(const unsigned char* mem1, size_t mem1_size, const unsigned char* mem2, size_t mem2_size) {
if (mem1 == NULL || mem2 == NULL) return 0;
if (mem1 == mem2 && mem1_size == mem2_size) return 1;
if (mem1_size != mem2_size) return 0;
for (int i = 0; i < mem1_size; i++) {
if (mem1[i] != mem2[i]) {
return 0;
}
}
return 1;
}
int memseq(const unsigned char* mem, size_t mem_size, const unsigned char c) {
if (mem == NULL) return 0;
for (int i = 0; i < mem_size; i++) {
if (mem[i] != c) {
return 0;
}
}
return 1;
}
pid_t gettid() {
return syscall(SYS_gettid);
}

View File

@ -1,54 +0,0 @@
/*
* util.h
*
* Created on: Nov 17, 2015
* Author: root
*/
#ifndef UTIL_H_
#define UTIL_H_
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
//#define MEM_LEAK_DEBUG
#ifdef MEM_LEAK_DEBUG
#define xmalloc(size) malloc(size)
#define xfree(ptr) free(ptr)
#define xcalloc(size) calloc(1, size)
#define xrealloc(ptr, size) realloc(ptr, size)
#else
void* xmalloc(size_t size);
void xfree(void* ptr);
void* xcalloc(size_t size);
void* xrealloc(void* ptr, size_t size);
#endif
void* xcopy(const void* ptr, size_t size, size_t expand);
char* xstrdup(const char* str, size_t expand);
char* xstrncat(char* dst, size_t maxsize, char* src);
int recur_mkdir(const char* path, mode_t mode);
int memeq(const unsigned char* mem1, size_t mem1_size, const unsigned char* mem2, size_t mem2_size);
int memseq(const unsigned char* mem, size_t mem_size, const unsigned char c);
pid_t gettid();
#endif /* UTIL_H_ */

View File

@ -1,289 +0,0 @@
/*
* xstring.c
*
* Created on: Nov 17, 2015
* Author: root
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "util.h"
#include "xstring.h"
char* trim(char* str) {
if (str == NULL) return NULL;
size_t len = strlen(str);
for (int i = len - 1; i >= 0; i--) {
if (isspace(str[i])) {
str[i] = 0;
} else break;
}
for (int i = 0; i < len; i++) {
if (!isspace(str[i])) return str + i;
}
return str + len;
}
int streq(const char* str1, const char* str2) {
if (str1 == NULL || str2 == NULL) return 0;
if (str1 == str2) return 1;
size_t l1 = strlen(str1);
size_t l2 = strlen(str2);
if (l1 != l2) return 0;
for (int i = 0; i < l1; i++) {
if (str1[i] != str2[i]) {
return 0;
}
}
return 1;
}
int streq_nocase(const char* str1, const char* str2) {
if (str1 == NULL || str2 == NULL) return 0;
if (str1 == str2) return 1;
size_t l1 = strlen(str1);
size_t l2 = strlen(str2);
if (l1 != l2) return 0;
for (int i = 0; i < l1; i++) {
char s1 = str1[i];
if (s1 >= 'A' && s1 <= 'Z') s1 += ' ';
char s2 = str2[i];
if (s2 >= 'A' && s2 <= 'Z') s2 += ' ';
if (s1 != s2) {
return 0;
}
}
return 1;
}
int startsWith(const char* str, const char* with) {
if (str == NULL || with == NULL) return 0;
if (str == with) return 1;
size_t l1 = strlen(str);
size_t l2 = strlen(with);
if (l1 < l2) return 0;
for (int i = 0; i < l2; i++) {
if (str[i] != with[i]) {
return 0;
}
}
return 1;
}
int startsWith_nocase(const char* str, const char* with) {
if (str == NULL || with == NULL) return 0;
if (str == with) return 1;
size_t l1 = strlen(str);
size_t l2 = strlen(with);
if (l1 < l2) return 0;
for (int i = 0; i < l2; i++) {
char s1 = str[i];
if (s1 >= 'A' && s1 <= 'Z') s1 += ' ';
char s2 = with[i];
if (s2 >= 'A' && s2 <= 'Z') s2 += ' ';
if (s1 != s2) {
return 0;
}
}
return 1;
}
int endsWith(const char* str, const char* with) {
if (str == NULL || with == NULL) return 0;
if (str == with) return 1;
size_t l1 = strlen(str);
size_t l2 = strlen(with);
if (l1 < l2) return 0;
for (int i = 0; i < l2; i++) {
if (str[l1 - 1 - (l2 - 1 - i)] != with[i]) {
return 0;
}
}
return 1;
}
int endsWith_nocase(const char* str, const char* with) {
if (str == NULL || with == NULL) return 0;
if (str == with) return 1;
size_t l1 = strlen(str);
size_t l2 = strlen(with);
if (l1 < l2) return 0;
for (int i = 0; i < l2; i++) {
char s1 = str[l1 - 1 - (l2 - 1 - i)];
if (s1 >= 'A' && s1 <= 'Z') s1 += ' ';
char s2 = with[i];
if (s2 >= 'A' && s2 <= 'Z') s2 += ' ';
if (s1 != s2) {
return 0;
}
}
return 1;
}
int contains(const char* str, const char* with) {
if (str == NULL || with == NULL) return 0;
if (str == with) return 1;
size_t l1 = strlen(str);
size_t l2 = strlen(with);
if (l1 < l2) return 0;
int ml = 0;
for (int i = 0; i < l1; i++) {
if (str[i] == with[ml]) {
if (++ml == l2) {
return 1;
}
} else ml = 0;
}
return 0;
}
int contains_nocase(const char* str, const char* with) {
if (str == NULL || with == NULL) return 0;
if (str == with) return 1;
size_t l1 = strlen(str);
size_t l2 = strlen(with);
if (l1 < l2) return 0;
int ml = 0;
for (int i = 0; i < l1; i++) {
char s1 = str[i];
if (s1 >= 'A' && s1 <= 'Z') s1 += ' ';
char s2 = with[ml];
if (s2 >= 'A' && s2 <= 'Z') s2 += ' ';
if (s1 == s2) {
if (++ml == l2) {
return 1;
}
} else ml = 0;
}
return 0;
}
char* toLowerCase(char* str) {
if (str == NULL) return NULL;
size_t l = strlen(str);
for (int i = 0; i < l; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') str[i] += ' ';
}
return str;
}
char* toUpperCase(char* str) {
if (str == NULL) return NULL;
size_t l = strlen(str);
for (int i = 0; i < l; i++) {
if (str[i] >= 'a' && str[i] <= 'z') str[i] -= ' ';
}
return str;
}
char* urlencode(char* str) {
size_t sl = strlen(str);
ssize_t off = 0;
for (size_t i = 0; i < sl; i++) {
char c = str[i];
if (c == '\"' || c == '#' || c == '$' || c == '%' || c == '&' || c == '+' || c == '-' || c == ',' || c == '/' || c == ':' || c == ';' || c == '=' || c == '?' || c == '@' || c == ' ' || c == '\t' || c == '>' || c == '<' || c == '{' || c == '}' || c == '|' || c == '\\' || c == '^' || c == '~' || c == '[' || c == ']' || c == '`') {
sl += 3;
str = xrealloc(str, sl + 1);
str[sl] = 0;
memmove(str + i + 3, str + i + 1, sl - i);
char sc[4];
snprintf(sc + 1, 3, "%02X", (uint8_t) c);
sc[0] = '%';
memcpy(str + i - 1 + 1, sc, 3);
off += (3 - 1);
i += (3 - 1);
}
}
return str;
}
char* replace(char* str, char* from, char* to) {
size_t sl = strlen(str);
size_t fl = strlen(from);
size_t tl = strlen(to);
size_t ml = 0;
for (size_t i = 0; i < sl; i++) {
char c = str[i];
if (c == from[ml]) {
if (++ml == fl) {
if (tl == fl) {
memcpy(str + i - fl + 1, to, tl);
} else if (tl < fl) {
memcpy(str + i - fl + 1, to, tl);
memmove(str + i + tl - fl + 1, str + i + fl - fl + 1, sl - i - fl + 1 + 1);
} else {
sl += (tl - fl);
str = xrealloc(str, sl);
memmove(str + i + tl - fl - 1, str + i - 1, sl - i + 1 + 1);
memcpy(str + i - fl + 1, to, tl);
}
i += (tl - fl);
ml = 0;
}
} else ml = 0;
}
return str;
}
char* replace_nocase(char* str, char* from, char* to) {
size_t sl = strlen(str);
size_t fl = strlen(from);
size_t tl = strlen(to);
size_t ml = 0;
for (size_t i = 0; i < sl; i++) {
char c = str[i];
if (c >= 'A' && c <= 'Z') c += ' ';
char c2 = from[ml];
if (c2 >= 'A' && c2 <= 'Z') c2 += ' ';
if (c == c2) {
if (++ml == fl) {
if (tl == fl) {
memcpy(str + i - fl + 1, to, tl);
} else if (tl < fl) {
memcpy(str + i - fl + 1, to, tl);
memmove(str + i + tl - fl + 1, str + i + fl - fl + 1, sl - i - fl + 1 + 1);
} else {
sl += (tl - fl);
str = xrealloc(str, sl);
memmove(str + i + tl - fl - 1, str + i - 1, sl - i + 1 + 1);
memcpy(str + i - fl + 1, to, tl);
}
i += (tl - fl);
ml = 0;
}
} else ml = 0;
}
return str;
}
int strisunum(const char* str) {
if (str == NULL) return 0;
size_t len = strlen(str);
if (len < 1) return 0;
for (int i = 0; i < len; i++) {
if (str[i] < '0' || str[i] > '9') {
return 0;
}
}
return 1;
}
size_t varstrlen(const char* fmt, const va_list args) {
va_list args_cpy;
va_copy(args_cpy, args);
size_t len = strlen(fmt) + 64;
for (const char* p = fmt; *p != '\0'; p++) {
if (*p != '%') continue;
switch (*++p) {
case 's':
len += strlen(va_arg(args_cpy, char *));
break;
default:
va_arg(args_cpy, int); // pop from list
}
}
return len;
}

View File

@ -1,46 +0,0 @@
/*
* xstring.h
*
* Created on: Nov 17, 2015
* Author: root
*/
#ifndef XSTRING_H_
#define XSTRING_H_
#include <string.h>
#include <stdarg.h>
char* trim(char* str);
int streq(const char* str1, const char* str2);
int streq_nocase(const char* str1, const char* str2);
int startsWith(const char* str, const char* with);
int startsWith_nocase(const char* str, const char* with);
int endsWith(const char* str, const char* with);
int endsWith_nocase(const char* str, const char* with);
int contains(const char* str, const char* with);
int contains_nocase(const char* str, const char* with);
char* toLowerCase(char* str);
char* toUpperCase(char* str);
char* urlencode(char* str); // must be freed and str must be on heap
char* replace(char* str, char* from, char* to); // when strlen(to) > strlen(from), str MUST be heap allocated!
char* replace_nocase(char* str, char* from, char* to);
int strisunum(const char* str);
size_t varstrlen(const char* fmt, const va_list args);
#endif /* XSTRING_H_ */

View File

@ -1,9 +1,9 @@
#ifndef AI_H_
#define AI_H_
#include "hashmap.h"
#include "world.h"
#include "entity.h"
#include <avuna/hash.h>
#include <basin/world.h>
#include <basin/entity.h>
#include <stdint.h>
struct ai_attackmelee_data {

View File

@ -1,6 +1,8 @@
#ifndef BASIN_ANTICHEAT_H_
#define BASIN_ANTICHEAT_H_
#include <basin/player.h>
int ac_chat(struct player* player, char* msg);
int ac_tick(struct player* player, int onground);
int ac_ticklook(struct player* player, float yaw, float pitch);

29
include/basin/basin.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef BASIN_BASIN_H_
#define BASIN_BASIN_H_
#include <basin/ai.h>
#include <basin/anticheat.h>
#include <basin/biome.h>
#include <basin/block.h>
#include <basin/command.h>
#include <basin/crafting.h>
#include <basin/entity.h>
#include <basin/game.h>
#include <basin/globals.h>
#include <basin/inventory.h>
#include <basin/item.h>
#include <basin/nbt.h>
#include <basin/network.h>
#include <basin/perlin.h>
#include <basin/player.h>
#include <basin/plugin.h>
#include <basin/profile.h>
#include <basin/server.h>
#include <basin/smelting.h>
#include <basin/tileentity.h>
#include <basin/tools.h>
#include <basin/version.h>
#include <basin/world.h>
#include <basin/worldmanager.h>
#endif //BASIN_BASIN_H_

View File

@ -8,9 +8,9 @@
#ifndef BLOCK_H_
#define BLOCK_H_
#include "world.h"
#include "player.h"
#include "item.h"
#include <basin/world.h>
#include <basin/player.h>
#include <basin/item.h>
#define BLK_AIR 0
#define BLK_STONE 16

View File

@ -8,7 +8,7 @@
#ifndef COMMAND_H_
#define COMMAND_H_
#include "player.h"
#include <basin/player.h>
#include <stdlib.h>
void init_base_commands();

View File

@ -8,8 +8,7 @@
#ifndef CRAFTING_H_
#define CRAFTING_H_
#include "collection.h"
#include "network.h"
#include <basin/network.h>
struct collection* crafting_recipies;

View File

@ -8,11 +8,11 @@
#ifndef ENTITY_H_
#define ENTITY_H_
#include <basin/world.h>
#include <basin/item.h>
#include <stdint.h>
#include "world.h"
#include <stdlib.h>
#include "world.h"
#include "item.h"
#define ENT_PLAYER 0
#define ENT_ITEM 1

View File

@ -8,9 +8,9 @@
#ifndef GAME_H_
#define GAME_H_
#include "world.h"
#include "entity.h"
#include "hashmap.h"
#include <basin/world.h>
#include <basin/entity.h>
#include <avuna/hash.h>
#include <pthread.h>
void player_openInventory(struct player* player, struct inventory* inv);

View File

@ -12,6 +12,8 @@
#define MC_PROTOCOL_VERSION_MAX 316
#define CHUNK_VIEW_DISTANCE 10
#include <avuna/log.h>
#include <avuna/hash.h>
#include <stdlib.h>
#include <pthread.h>

View File

@ -8,8 +8,8 @@
#ifndef INVENTORY_H_
#define INVENTORY_H_
#include <avuna/hash.h>
#include <stdint.h>
#include "hashmap.h"
#include <pthread.h>
#define INVTYPE_PLAYERINVENTORY 0

View File

@ -12,11 +12,11 @@
typedef int16_t item;
#include "player.h"
#include "world.h"
#include "entity.h"
#include "inventory.h"
#include "tools.h"
#include <basin/player.h>
#include <basin/world.h>
#include <basin/entity.h>
#include <basin/inventory.h>
#include <basin/tools.h>
void offsetCoordByFace(int32_t* x, int32_t* y, int32_t* z, uint8_t face);

View File

@ -8,8 +8,7 @@
#ifndef NETWORK_H_
#define NETWORK_H_
#include "accept.h"
#include "inventory.h"
#include <basin/inventory.h>
#include <openssl/rsa.h>
struct packet;

View File

@ -8,8 +8,8 @@
#ifndef PLAYER_H_
#define PLAYER_H_
#include "network.h"
#include "block.h"
#include <basin/network.h>
#include <basin/block.h>
typedef struct _acstate {
} acstate_t;

View File

@ -8,10 +8,10 @@
#ifndef PLUGIN_H_
#define PLUGIN_H_
#include "hashmap.h"
#include "world.h"
#include <basin/world.h>
#include <basin/block.h>
#include <avuna/hash.h>
#include <stdint.h>
#include "block.h"
#include <jni.h>
struct hashmap* plugins;

View File

@ -14,6 +14,5 @@ int online_mode;
int difficulty;
#define RANDOM_TICK_SPEED 3
// 3
#endif /* SERVER_H_ */

View File

@ -8,10 +8,10 @@
#ifndef SMELTING_H_
#define SMELTING_H_
#include <basin/world.h>
#include <basin/entity.h>
#include <basin/inventory.h>
#include <stdint.h>
#include "world.h"
#include "entity.h"
#include "inventory.h"
struct smelting_fuel {
int16_t id;

View File

@ -8,8 +8,8 @@
#ifndef TILEENTITY_H_
#define TILEENTITY_H_
#include "nbt.h"
#include "inventory.h"
#include <basin/nbt.h>
#include <basin/inventory.h>
struct world;

33
include/basin/tools.h Normal file
View File

@ -0,0 +1,33 @@
/*
* tools.h
*
* Created on: Dec 26, 2016
* Author: root
*/
#ifndef TOOLS_H_
#define TOOLS_H_
#include <basin/item.h>
#include <basin/world.h>
struct tool_proficiency {
uint16_t* proficient;
size_t proficient_size;
};
struct tool_info {
char* name;
struct tool_proficiency* proficiencies;
size_t proficient_size;
};
void tools_init();
struct tool_info* tools_get(char* name);
int tools_proficient(struct tool_info* info, uint8_t harvest_level, uint16_t b);
void tools_add(struct tool_info* tool);
#endif /* TOOLS_H_ */

View File

@ -8,11 +8,11 @@
#ifndef WORLD_H_
#define WORLD_H_
#include <basin/network.h>
#include <basin/inventory.h>
#include <basin/perlin.h>
#include <stdint.h>
#include "network.h"
#include "inventory.h"
#include <pthread.h>
#include "perlin.h"
typedef uint16_t block;

View File

@ -8,7 +8,7 @@
#ifndef WORLDMANAGER_H_
#define WORLDMANAGER_H_
#include "world.h"
#include <basin/world.h>
#define NETHER -1
#define OVERWORLD 0

View File

@ -5,20 +5,19 @@
* Author: root
*/
#include "accept.h"
#include "util.h"
#include "work.h"
#include <basin/queue.h>
#include <avuna/string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include "xstring.h"
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <poll.h>
#include "work.h"
#include <unistd.h>
#include "queue.h"
#include <pthread.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
void run_accept(struct accept_param* param) {
static int one = 1;

View File

@ -8,13 +8,12 @@
#ifndef ACCEPT_H_
#define ACCEPT_H_
#include "config.h"
#include "collection.h"
#include <sys/socket.h>
#include "work.h"
#include <netinet/ip6.h>
#include <avuna/config.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <sys/socket.h>
#include <netinet/ip6.h>
struct accept_param {
int server_fd;

View File

@ -1,9 +1,9 @@
#include "ai.h"
#include "entity.h"
#include "world.h"
#include "hashmap.h"
#include "util.h"
#include "game.h"
#include <basin/ai.h>
#include <basin/entity.h>
#include <basin/world.h>
#include <basin/game.h>
#include <avuna/hash.h>
#include <math.h>
void initai_zombie(struct world* world, struct entity* entity) {

View File

@ -1,7 +1,8 @@
#include <basin/basin.h>
#include <basin/profile.h>
#include <basin/anticheat.h>
#include <math.h>
#include "basin.h"
#include "profile.h"
#include "anticheat.h"
#define AC_BEGIN beginProfilerSection("player_anticheat");
#define AC_END(ret) { endProfilerSection("player_anticheat"); if (ret) printf("[AC] Player %s infraction at %s (%d)\n", player->name, __func__, ret); return ret; }

View File

@ -5,25 +5,24 @@
* Author: root
*/
#include "util.h"
#include "inventory.h"
#include "globals.h"
#include "network.h"
#include "packet.h"
#include "world.h"
#include "game.h"
#include "xstring.h"
#include "queue.h"
#include "smelting.h"
#include "tileentity.h"
#include "smelting.h"
#include <fcntl.h>
#include <errno.h>
#include "json.h"
#include "item.h"
#include "block.h"
#include <basin/inventory.h>
#include <basin/globals.h>
#include "basin/network.h"
#include <basin/world.h>
#include <basin/game.h>
#include <basin/queue.h>
#include <basin/smelting.h>
#include <basin/tileentity.h>
#include <basin/smelting.h>
#include <basin/item.h>
#include <basin/block.h>
#include <avuna/json.h>
#include <avuna/string.h>
#include <unistd.h>
#include <math.h>
#include <fcntl.h>
#include <errno.h>
struct collection* block_infos;
struct collection* block_materials;
@ -1719,39 +1718,39 @@ void init_materials() {
}
close(fd);
struct json_object json;
parseJSON(&json, jsf);
json_parse(&json, jsf);
for (size_t i = 0; i < json.child_count; i++) {
struct json_object* ur = json.children[i];
struct block_material* bm = xcalloc(sizeof(struct block_material));
bm->name = xstrdup(ur->name, 0);
struct json_object* tmp = getJSONValue(ur, "flammable");
struct json_object* tmp = json_get(ur, "flammable");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->flammable = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "replaceable");
tmp = json_get(ur, "replaceable");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->replacable = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "requiresnotool");
tmp = json_get(ur, "requiresnotool");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->requiresnotool = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "mobility");
tmp = json_get(ur, "mobility");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bm->mobility = (uint8_t) tmp->data.number;
tmp = getJSONValue(ur, "adventure_exempt");
tmp = json_get(ur, "adventure_exempt");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->adventure_exempt = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "liquid");
tmp = json_get(ur, "liquid");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->liquid = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "solid");
tmp = json_get(ur, "solid");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->solid = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "blocksLight");
tmp = json_get(ur, "blocksLight");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->blocksLight = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "blocksMovement");
tmp = json_get(ur, "blocksMovement");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->blocksMovement = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "opaque");
tmp = json_get(ur, "opaque");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bm->opaque = tmp->type == JSON_TRUE;
add_block_material(bm);
@ -1787,74 +1786,74 @@ void init_blocks() {
}
close(fd);
struct json_object json;
parseJSON(&json, jsf);
json_parse(&json, jsf);
for (size_t i = 0; i < json.child_count; i++) {
struct json_object* ur = json.children[i];
struct block_info* bi = xcalloc(sizeof(struct block_info));
struct json_object* tmp = getJSONValue(ur, "id");
struct json_object* tmp = json_get(ur, "id");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
block id = (block) tmp->data.number;
if (id < 0) goto cerr;
struct json_object* colls = getJSONValue(ur, "collision");
struct json_object* colls = json_get(ur, "collision");
if (colls == NULL || colls->type != JSON_OBJECT) goto cerr;
bi->boundingBox_count = colls->child_count;
bi->boundingBoxes = colls->child_count == 0 ? NULL : xmalloc(sizeof(struct boundingbox) * colls->child_count);
for (size_t x = 0; x < colls->child_count; x++) {
struct json_object* coll = colls->children[x];
struct boundingbox* bb = &bi->boundingBoxes[x];
tmp = getJSONValue(coll, "minX");
tmp = json_get(coll, "minX");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bb->minX = (double) tmp->data.number;
tmp = getJSONValue(coll, "maxX");
tmp = json_get(coll, "maxX");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bb->maxX = (double) tmp->data.number;
tmp = getJSONValue(coll, "minY");
tmp = json_get(coll, "minY");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bb->minY = (double) tmp->data.number;
tmp = getJSONValue(coll, "maxY");
tmp = json_get(coll, "maxY");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bb->maxY = (double) tmp->data.number;
tmp = getJSONValue(coll, "minZ");
tmp = json_get(coll, "minZ");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bb->minZ = (double) tmp->data.number;
tmp = getJSONValue(coll, "maxZ");
tmp = json_get(coll, "maxZ");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bb->maxZ = (double) tmp->data.number;
}
tmp = getJSONValue(ur, "dropItem");
tmp = json_get(ur, "dropItem");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->drop = (item) tmp->data.number;
tmp = getJSONValue(ur, "dropDamage");
tmp = json_get(ur, "dropDamage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->drop_damage = (int16_t) tmp->data.number;
tmp = getJSONValue(ur, "dropAmountMin");
tmp = json_get(ur, "dropAmountMin");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->drop_min = (uint8_t) tmp->data.number;
tmp = getJSONValue(ur, "dropAmountMax");
tmp = json_get(ur, "dropAmountMax");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->drop_max = (uint8_t) tmp->data.number;
tmp = getJSONValue(ur, "hardness");
tmp = json_get(ur, "hardness");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->hardness = (float) tmp->data.number;
tmp = getJSONValue(ur, "material");
tmp = json_get(ur, "material");
if (tmp == NULL || tmp->type != JSON_STRING) goto cerr;
bi->material = getBlockMaterial(tmp->data.string);
tmp = getJSONValue(ur, "slipperiness");
tmp = json_get(ur, "slipperiness");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->slipperiness = (float) tmp->data.number;
tmp = getJSONValue(ur, "isFullCube");
tmp = json_get(ur, "isFullCube");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bi->fullCube = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "canProvidePower");
tmp = json_get(ur, "canProvidePower");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
bi->canProvidePower = tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "lightOpacity");
tmp = json_get(ur, "lightOpacity");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->lightOpacity = (uint8_t) tmp->data.number;
tmp = getJSONValue(ur, "lightEmission");
tmp = json_get(ur, "lightEmission");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
bi->lightEmission = (uint8_t) tmp->data.number;
tmp = getJSONValue(ur, "resistance");
tmp = json_get(ur, "resistance");
if (tmp == NULL || tmp->type != JSON_NUMBER) {
bi->resistance = bi->hardness * 5;
} else {

View File

@ -5,17 +5,15 @@
* Author: root
*/
#include <basin/game.h>
#include <basin/player.h>
#include <basin/globals.h>
#include <basin/command.h>
#include <basin/profile.h>
#include <basin/server.h>
#include <basin/item.h>
#include <avuna/string.h>
#include <stdlib.h>
#include "collection.h"
#include "game.h"
#include "player.h"
#include "globals.h"
#include "xstring.h"
#include "util.h"
#include "command.h"
#include "profile.h"
#include "server.h"
#include "item.h"
void command_gamemode(struct player* player, char** args, size_t args_count) {
if (player != NULL) return;

View File

@ -5,19 +5,17 @@
* Author: root
*/
#include "crafting.h"
#include "network.h"
#include "item.h"
#include "block.h"
#include <basin/crafting.h>
#include <basin/network.h>
#include <basin/item.h>
#include <basin/block.h>
#include <basin/player.h>
#include <basin/game.h>
#include <avuna/string.h>
#include <avuna/json.h>
#include <fcntl.h>
#include "xstring.h"
#include <errno.h>
#include "util.h"
#include "json.h"
#include "player.h"
#include <unistd.h>
#include "game.h"
#include <errno.h>
void init_crafting() {
crafting_recipies = new_collection(128, 0);
@ -39,26 +37,26 @@ void init_crafting() {
}
close(fd);
struct json_object json;
parseJSON(&json, jsf);
json_parse(&json, jsf);
for (size_t i = 0; i < json.child_count; i++) {
struct json_object* ur = json.children[i];
struct crafting_recipe* cr = xcalloc(sizeof(struct crafting_recipe));
struct json_object* tmp = getJSONValue(ur, "shapeless");
struct json_object* tmp = json_get(ur, "shapeless");
if (tmp == NULL || (tmp->type != JSON_TRUE && tmp->type != JSON_FALSE)) goto cerr;
cr->shapeless = (uint8_t) tmp->type == JSON_TRUE;
tmp = getJSONValue(ur, "width");
tmp = json_get(ur, "width");
if ((tmp == NULL || tmp->type != JSON_NUMBER) && !cr->shapeless) goto cerr;
cr->width = cr->shapeless ? 0 : (uint8_t) tmp->data.number;
for (size_t x = 0; x < ur->child_count; x++) {
struct json_object* ir = ur->children[x];
if (streq_nocase(ir->name, "output")) {
tmp = getJSONValue(ir, "item_id");
tmp = json_get(ir, "item_id");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
cr->output.item = (int16_t) tmp->data.number;
tmp = getJSONValue(ir, "item_damage");
tmp = json_get(ir, "item_damage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
cr->output.damage = (int16_t) tmp->data.number;
tmp = getJSONValue(ir, "item_count");
tmp = json_get(ir, "item_count");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
cr->output.itemCount = (uint8_t) tmp->data.number;
cr->output.nbt = NULL;
@ -66,10 +64,10 @@ void init_crafting() {
uint8_t id = atoi(ir->name + 5);
if (id > 8) goto cerr;
cr->slot[id] = xmalloc(sizeof(struct slot));
tmp = getJSONValue(ir, "item_id");
tmp = json_get(ir, "item_id");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
cr->slot[id]->item = (int16_t) tmp->data.number;
tmp = getJSONValue(ir, "item_damage");
tmp = json_get(ir, "item_damage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
cr->slot[id]->damage = (int16_t) tmp->data.number;
cr->slot[id]->itemCount = 1;

View File

@ -5,26 +5,25 @@
* Author: root
*/
#include "packet.h"
#include <basin/basin.h>
#include <basin/network.h>
#include <basin/queue.h>
#include <basin/entity.h>
#include <basin/ai.h>
#include <basin/game.h>
#include <basin/plugin.h>
#include <avuna/string.h>
#include <avuna/json.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#include "basin.h"
#include "network.h"
#include "packet.h"
#include "util.h"
#include "xstring.h"
#include "queue.h"
#include "entity.h"
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include "json.h"
#include <errno.h>
#include "ai.h"
#include "game.h"
#include "plugin.h"
void onSpawned_minecart(struct world* world, struct entity* entity) {
if (entity->type == ENT_MINECARTRIDEABLE) entity->objectData = 0;
@ -373,24 +372,24 @@ void init_entities() {
}
close(fd);
struct json_object json;
parseJSON(&json, jsf);
json_parse(&json, jsf);
for (size_t i = 0; i < json.child_count; i++) {
struct json_object* ur = json.children[i];
struct entity_info* ei = xcalloc(sizeof(struct entity_info));
struct json_object* tmp = getJSONValue(ur, "id");
struct json_object* tmp = json_get(ur, "id");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
uint32_t id = (block) tmp->data.number;
if (id < 0) goto cerr;
tmp = getJSONValue(ur, "maxHealth");
tmp = json_get(ur, "maxHealth");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ei->maxHealth = (float) tmp->data.number;
tmp = getJSONValue(ur, "width");
tmp = json_get(ur, "width");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ei->width = (float) tmp->data.number;
tmp = getJSONValue(ur, "height");
tmp = json_get(ur, "height");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ei->height = (float) tmp->data.number;
tmp = getJSONValue(ur, "loot");
tmp = json_get(ur, "loot");
if (tmp != NULL && tmp->type == JSON_ARRAY) {
ei->loot_count = tmp->child_count;
ei->loots = xcalloc(tmp->child_count * sizeof(struct entity_loot));
@ -400,14 +399,14 @@ void init_entities() {
xfree(ei->loots);
goto cerr;
}
struct json_object* emp = getJSONValue(fj, "id");
struct json_object* emp = json_get(fj, "id");
if (emp == NULL || emp->type != JSON_NUMBER) {
xfree(ei->loots);
goto cerr;
}
ei->loots[i].id = (item) emp->data.number;
struct json_object* amin = getJSONValue(fj, "amountMin");
struct json_object* amax = getJSONValue(fj, "amountMax");
struct json_object* amin = json_get(fj, "amountMin");
struct json_object* amax = json_get(fj, "amountMax");
if ((amin == NULL) != (amax == NULL) || (amin != NULL && (amin->type != JSON_NUMBER || amax->type != JSON_NUMBER))) {
xfree(ei->loots);
goto cerr;
@ -416,8 +415,8 @@ void init_entities() {
ei->loots[i].amountMin = (uint8_t) amin->data.number;
ei->loots[i].amountMax = (uint8_t) amax->data.number;
}
amin = getJSONValue(fj, "metaMin");
amax = getJSONValue(fj, "metaMax");
amin = json_get(fj, "metaMin");
amax = json_get(fj, "metaMax");
if ((amin == NULL) != (amax == NULL) || (amin != NULL && (amin->type != JSON_NUMBER || amax->type != JSON_NUMBER))) {
xfree(ei->loots);
goto cerr;
@ -428,7 +427,7 @@ void init_entities() {
}
}
}
tmp = getJSONValue(ur, "flags");
tmp = json_get(ur, "flags");
if (tmp == NULL || tmp->type != JSON_ARRAY) goto cerr;
ei->flag_count = tmp->child_count;
ei->flags = xmalloc(tmp->child_count * sizeof(char*));
@ -437,16 +436,16 @@ void init_entities() {
if (fj == NULL || fj->type != JSON_STRING) goto cerr;
ei->flags[i] = toLowerCase(xstrdup(trim(fj->data.string), 0));
}
tmp = getJSONValue(ur, "packetType");
tmp = json_get(ur, "packetType");
if (tmp == NULL || tmp->type != JSON_STRING) goto cerr;
if (streq_nocase(tmp->data.string, "mob")) ei->spawn_packet = PKT_PLAY_CLIENT_SPAWNMOB;
else if (streq_nocase(tmp->data.string, "object")) ei->spawn_packet = PKT_PLAY_CLIENT_SPAWNOBJECT;
else if (streq_nocase(tmp->data.string, "exp")) ei->spawn_packet = PKT_PLAY_CLIENT_SPAWNEXPERIENCEORB;
else if (streq_nocase(tmp->data.string, "painting")) ei->spawn_packet = PKT_PLAY_CLIENT_SPAWNPAINTING;
tmp = getJSONValue(ur, "packetID");
tmp = json_get(ur, "packetID");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ei->spawn_packet_id = (int32_t) tmp->data.number;
tmp = getJSONValue(ur, "dataname");
tmp = json_get(ur, "dataname");
if (tmp == NULL || tmp->type != JSON_STRING) goto cerr;
ei->dataname = xstrdup(tmp->data.string, 0);
add_entity_info(id, ei);

View File

@ -5,29 +5,26 @@
* Author: root
*/
#include "basin.h"
#include "util.h"
#include "queue.h"
#include "collection.h"
#include "packet.h"
#include <errno.h>
#include <stdint.h>
#include "xstring.h"
#include <stdio.h>
#include "game.h"
#include <math.h>
#include "crafting.h"
#include "smelting.h"
#include "tileentity.h"
#include "network.h"
#include "server.h"
#include "command.h"
#include <basin/basin.h>
#include <basin/queue.h>
#include <basin/game.h>
#include <basin/crafting.h>
#include <basin/smelting.h>
#include <basin/tileentity.h>
#include <basin/network.h>
#include <basin/server.h>
#include <basin/command.h>
#include <basin/profile.h>
#include <basin/anticheat.h>
#include <avuna/hash.h>
#include <avuna/string.h>
#include <unistd.h>
#include <stdarg.h>
#include "xstring.h"
#include "hashmap.h"
#include "profile.h"
#include "anticheat.h"
#include <math.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
void flush_outgoing(struct player* player) {
if (player->conn == NULL) return;
@ -306,7 +303,7 @@ void dropBlockDrops(struct world* world, block blk, struct player* breaker, int3
struct slot* ci = getSlot(breaker, breaker->inventory, 36 + breaker->currentItem);
if (ci != NULL) {
struct item_info* ii = getItemInfo(ci->item);
if (ii != NULL) badtool = !isToolProficient(ii->toolType, ii->harvestLevel, blk);
if (ii != NULL) badtool = !tools_proficient(ii->toolType, ii->harvestLevel, blk);
}
}
if (badtool) return;

View File

@ -5,22 +5,21 @@
* Author: root
*/
#include "network.h"
#include "packet.h"
#include "globals.h"
#include "item.h"
#include <basin/network.h>
#include <basin/globals.h>
#include <basin/item.h>
#include <basin/game.h>
#include <basin/queue.h>
#include <basin/player.h>
#include <basin/smelting.h>
#include <basin/nbt.h>
#include <basin/tileentity.h>
#include <basin/inventory.h>
#include <avuna/hash.h>
#include <string.h>
#include <math.h>
#include "util.h"
#include "game.h"
#include "queue.h"
#include "player.h"
#include "smelting.h"
#include "nbt.h"
#include <stdint.h>
#include "tileentity.h"
#include "inventory.h"
#include "hashmap.h"
#include <unistd.h>
void newInventory(struct inventory* inv, int type, int id, int slots) {

View File

@ -5,19 +5,18 @@
* Author: root
*/
#include "item.h"
#include "collection.h"
#include "json.h"
#include <basin/item.h>
#include <basin/block.h>
#include <basin/nbt.h>
#include <basin/game.h>
#include <basin/player.h>
#include <avuna/json.h>
#include <avuna/string.h>
#include <fcntl.h>
#include "xstring.h"
#include <errno.h>
#include "util.h"
#include <unistd.h>
#include "block.h"
#include "nbt.h"
#include <math.h>
#include "game.h"
#include "player.h"
int onItemInteract_bed(struct world* world, struct player* player, uint8_t slot_index, struct slot* slot, int32_t x, int32_t y, int32_t z, uint8_t face) {
if (face != YP) return 0;
@ -714,38 +713,38 @@ void init_items() {
}
close(fd);
struct json_object json;
parseJSON(&json, jsf);
json_parse(&json, jsf);
for (size_t i = 0; i < json.child_count; i++) {
struct json_object* ur = json.children[i];
struct item_info* ii = xcalloc(sizeof(struct item_info));
struct json_object* tmp = getJSONValue(ur, "id");
struct json_object* tmp = json_get(ur, "id");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
item id = (item) tmp->data.number;
if (id < 0) goto cerr;
tmp = getJSONValue(ur, "toolType");
tmp = json_get(ur, "toolType");
if (tmp == NULL || tmp->type != JSON_STRING) goto cerr;
if (streq_nocase(tmp->data.string, "none")) ii->toolType = NULL;
else ii->toolType = getToolInfo(tmp->data.string);
tmp = getJSONValue(ur, "harvestLevel");
else ii->toolType = tools_get(tmp->data.string);
tmp = json_get(ur, "harvestLevel");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->harvestLevel = (uint8_t) tmp->data.number;
tmp = getJSONValue(ur, "maxStackSize");
tmp = json_get(ur, "maxStackSize");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->maxStackSize = (uint8_t) tmp->data.number;
if (ii->maxStackSize > 64) goto cerr;
tmp = getJSONValue(ur, "maxDamage");
tmp = json_get(ur, "maxDamage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->maxDamage = (int16_t) tmp->data.number;
tmp = getJSONValue(ur, "maxUseDuration");
tmp = json_get(ur, "maxUseDuration");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->maxUseDuration = (uint32_t) tmp->data.number;
tmp = getJSONValue(ur, "toolProficiency");
tmp = json_get(ur, "toolProficiency");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->toolProficiency = (float) tmp->data.number;
tmp = getJSONValue(ur, "damage");
tmp = json_get(ur, "damage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->damage = (float) tmp->data.number;
tmp = getJSONValue(ur, "armorType");
tmp = json_get(ur, "armorType");
if (tmp == NULL || tmp->type != JSON_STRING) goto cerr;
if (streq_nocase(tmp->data.string, "none")) ii->armorType = ARMOR_NONE;
else if (streq_nocase(tmp->data.string, "helmet")) ii->armorType = ARMOR_HELMET;
@ -753,10 +752,10 @@ void init_items() {
else if (streq_nocase(tmp->data.string, "leggings")) ii->armorType = ARMOR_LEGGINGS;
else if (streq_nocase(tmp->data.string, "boots")) ii->armorType = ARMOR_BOOTS;
else goto cerr;
tmp = getJSONValue(ur, "armor");
tmp = json_get(ur, "armor");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->armor = (uint8_t) tmp->data.number;
tmp = getJSONValue(ur, "attackSpeed");
tmp = json_get(ur, "attackSpeed");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->attackSpeed = (float) tmp->data.number;
ensure_collection(item_infos, id + 1);

View File

@ -5,17 +5,28 @@
* Author: root
*/
#include <unistd.h>
#include <stdio.h>
#include "config.h"
#include <errno.h>
#include "xstring.h"
#include "version.h"
#include "util.h"
#include "accept.h"
#include "work.h"
#include <basin/world.h>
#include <basin/worldmanager.h>
#include <basin/server.h>
#include <basin/game.h>
#include <basin/block.h>
#include <basin/crafting.h>
#include <basin/tools.h>
#include <basin/smelting.h>
#include <basin/command.h>
#include <basin/queue.h>
#include <basin/profile.h>
#include <basin/plugin.h>
#include <basin/version.h>
#include <basin/globals.h>
#include <avuna/config.h>
#include <avuna/string.h>
#include <avuna/streams.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include "streams.h"
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -24,25 +35,12 @@
#include <netinet/ip6.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "accept.h"
#include "globals.h"
#include "collection.h"
#include "work.h"
#include <sys/types.h>
#include <time.h>
#include "world.h"
#include "worldmanager.h"
#include "server.h"
#include "game.h"
#include "block.h"
#include "crafting.h"
#include "tools.h"
#include "smelting.h"
#include "command.h"
#include "queue.h"
#include "profile.h"
#include "plugin.h"
#include <openssl/rand.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
void main_tick() {
pthread_cond_broadcast (&glob_tick_cond);
@ -96,6 +94,8 @@ void main_tick_thread(void* ptr) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
double lastRun = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while (1) {
clock_gettime(CLOCK_MONOTONIC, &ts);
double ct = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
@ -111,6 +111,7 @@ void main_tick_thread(void* ptr) {
}
usleep((lastRun - (ct - 50.)) * 1000.);
}
#pragma clang diagnostic pop
}
int main(int argc, char* argv[]) {
@ -200,7 +201,7 @@ int main(int argc, char* argv[]) {
printf("Entities Initialized\n");
init_crafting();
printf("Crafting Initialized\n");
init_tools();
tools_init();
printf("Tools Initialized\n");
init_items();
printf("Items Initialized\n");

View File

@ -5,13 +5,13 @@
* Author: root
*/
#include "nbt.h"
#include <basin/nbt.h>
#include <basin/network.h>
#include <avuna/string.h>
#include <stdlib.h>
#include "xstring.h"
#include "util.h"
#include <zlib.h>
#include <stdio.h>
#include "network.h"
void freeNBT(struct nbt_tag* nbt) {
if (nbt->name != NULL) xfree(nbt->name);

View File

@ -5,29 +5,29 @@
* Author: root
*/
#include "accept.h"
#include "packet.h"
#include <basin/nbt.h>
#include <basin/network.h>
#include <avuna/pmem.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/ssl.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "nbt.h"
#include <zlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include "accept.h"
#include "packet.h"
#include "util.h"
#include <stdint.h>
#include <stdlib.h>
#include "network.h"
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/ssl.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
void init_encryption() {
public_rsa = RSA_new();

View File

@ -1,18 +1,18 @@
#include "globals.h"
#include <basin/globals.h>
#include "inventory.h"
#include <basin/inventory.h>
#include <stdint.h>
#include "network.h"
#include "basin/network.h"
#include <zlib.h>
#include <errno.h>
#include "xstring.h"
#include <avuna/string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "nbt.h"
#include <basin/nbt.h>
#include <zlib.h>
#include <errno.h>
#include <string.h>
@ -20,16 +20,16 @@
#include <time.h>
#include "accept.h"
#include "util.h"
#include "world.h"
#include <basin/world.h>
#include <math.h>
#include "packet.h"
#include "player.h"
#include <basin/player.h>
#define ADRX if(rx == 0) goto rer;pbuf += rx;ps -= rx;
#define ADX(x) pbuf += x;ps -= x;
#define CPS(x) if(ps < x) goto rer;
#define CPS_OPT(x) if(ps >= x) {
#define ENS(x) if(ps-pi < x) { ps += (x > 256 ? x + 1024 : 1024); pktbuf = xrealloc(pktbuf - 10, ps + 10) + 10; }
#define ENS(x) if(ps-pi < x) { ps += (x > 256 ? x + 1024 : 1024); pktbuf = prealloc(pktbuf - 10, ps + 10) + 10; }
ssize_t readPacket(struct conn* conn, unsigned char* buf, size_t buflen, struct packet* packet) {
void* pktbuf = buf;

View File

@ -1,7 +1,7 @@
#ifndef PACKET_H_
#define PACKET_H_
#include "network.h"
#include "basin/network.h"
#define STATE_HANDSHAKE 0
#define STATE_PLAY 3

View File

@ -1,4 +1,4 @@
#include "perlin.h"
#include <basin/perlin.h>
#include <stdint.h>
#include <math.h>

View File

@ -5,24 +5,22 @@
* Author: root
*/
#include "network.h"
#include "accept.h"
#include "util.h"
#include "inventory.h"
#include "xstring.h"
#include "queue.h"
#include "packet.h"
#include "collection.h"
#include "tileentity.h"
#include <basin/network.h>
#include <basin/inventory.h>
#include <basin/queue.h>
#include <basin/tileentity.h>
#include <basin/server.h>
#include <basin/profile.h>
#include <basin/basin.h>
#include <basin/anticheat.h>
#include <basin/command.h>
#include <basin/smelting.h>
#include <basin/crafting.h>
#include <basin/plugin.h>
#include <avuna/string.h>
#include <math.h>
#include "server.h"
#include "profile.h"
#include "basin.h"
#include "anticheat.h"
#include "command.h"
#include "smelting.h"
#include "crafting.h"
#include "plugin.h"
struct player* newPlayer(struct entity* entity, char* name, struct uuid uuid, struct conn* conn, uint8_t gamemode) {
struct player* player = xmalloc(sizeof(struct player));
@ -57,7 +55,7 @@ struct player* newPlayer(struct entity* entity, char* name, struct uuid uuid, st
player->inventory = xmalloc(sizeof(struct inventory));
newInventory(player->inventory, INVTYPE_PLAYERINVENTORY, 0, 46);
player->openInv = NULL;
put_hashmap(player->inventory->players, player->entity->id, player);
hashmap_put(player->inventory->players, player->entity->id, player);
player->loadedChunks = new_hashmap(1, 1);
player->loadedEntities = new_hashmap(1, 0);
player->incomingPacket = new_queue(0, 1);
@ -1287,8 +1285,8 @@ void tick_player(struct world* world, struct player* player) {
pthread_mutex_lock(&player->inventory->mut);
struct slot* ci = getSlot(player, player->inventory, 36 + player->currentItem);
struct item_info* cii = ci == NULL ? NULL : getItemInfo(ci->item);
int hasProperTool = (cii == NULL ? 0 : isToolProficient(cii->toolType, cii->harvestLevel, bw));
int hasProperTool2 = (cii == NULL ? 0 : isToolProficient(cii->toolType, 0xFF, bw));
int hasProperTool = (cii == NULL ? 0 : tools_proficient(cii->toolType, cii->harvestLevel, bw));
int hasProperTool2 = (cii == NULL ? 0 : tools_proficient(cii->toolType, 0xFF, bw));
pthread_mutex_unlock(&player->inventory->mut);
int rnt = bi->material->requiresnotool;
float ds = hasProperTool2 ? cii->toolProficiency : 1.;

View File

@ -5,20 +5,23 @@
* Author: root
*/
#include "plugin.h"
#include <basin/plugin.h>
#include <avuna/hash.h>
#include <avuna/string.h>
#include <avuna/pmem.h>
#include <dirent.h>
#include "hashmap.h"
#include <stdint.h>
#include <dlfcn.h>
#include "util.h"
#include "xstring.h"
#include <errno.h>
#include <jni.h>
uint32_t next_plugin_id;
struct mempool* plugin_pool;
void init_plugins() {
plugins = new_hashmap(1, 1);
plugin_pool = mempool_new();
plugins = hashmap_new(16, plugin_pool);
DIR* dir = opendir("plugins/");
char lp[PATH_MAX];
uint32_t lua_count = 0;
@ -27,7 +30,7 @@ void init_plugins() {
struct dirent* de = NULL;
while ((de = readdir(dir)) != NULL) {
snprintf(lp, PATH_MAX, "plugins/%s", de->d_name);
if (endsWith_nocase(de->d_name, ".so")) { // BASIN C
if (str_suffixes(de->d_name, ".so")) { // BASIN C
struct plugin* pl = xcalloc(sizeof(struct plugin));
pl->hnd = dlopen(lp, RTLD_GLOBAL | RTLD_NOW);
if (pl->hnd == NULL) {
@ -40,7 +43,7 @@ void init_plugins() {
(*init)(pl);
pl->type = PLUGIN_BASIN;
put_hashmap(plugins, next_plugin_id++, pl);
} else if (endsWith_nocase(de->d_name, ".jar")) { // BUKKIT
} else if (str_suffixes(de->d_name, ".jar")) { // BUKKIT
struct plugin* pl = xcalloc(sizeof(struct plugin));
pl->type = PLUGIN_BUKKIT;
char* cp = xmalloc(strlen(lp) + strlen("-Djava.class.path=bukkit.jar:basinBukkit.jar:") + 1);

115
src/profile.c Normal file
View File

@ -0,0 +1,115 @@
/*
* profile.c
*
* Created on: Dec 30, 2016
* Author: root
*/
#include <avuna/string.h>
#include <avuna/hash.h>
#include <avuna/pmem.h>
#include <avuna/util.h>
#include <time.h>
#include <stdio.h>
#define ENABLE_PROFILER
// #define ENABLE_PROFILER_MT
struct mempool* profiler_pool;
struct hashmap* profiler_sections;
struct profiler_section {
char* name;
double ms_spent;
double sectionLastStart;
double creation;
double maxTime;
};
void beginProfilerSection(char* name) {
#ifdef ENABLE_PROFILER
if (profiler_sections == NULL) {
profiler_pool = mempool_new();
#ifdef ENABLE_PROFILER_MT
profiler_sections = hashmap_new_thread(32, profiler_pool);
#else
profiler_sections = hashmap_new(32, profiler_pool);
#endif
}
struct profiler_section* section = hashmap_get(profiler_sections, name);
if (section != NULL) {
if (section->sectionLastStart > 0.) return;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
section->sectionLastStart = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
} else {
section = pcalloc(profiler_pool, sizeof(struct profiler_section));
section->name = name;
hashmap_put(profiler_sections, name, section);
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
section->sectionLastStart = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
section->creation = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
}
#endif
}
void endProfilerSection(char* name) {
#ifdef ENABLE_PROFILER
if (profiler_sections == NULL) return;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
double now = ((double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.);
struct profiler_section* section = hashmap_get(profiler_sections, name);
if (section == NULL) return;
if (section->sectionLastStart < 0.) return;
double msa = now - section->sectionLastStart;
section->ms_spent += msa;
section->sectionLastStart = -1.;
if (section->maxTime < msa) {
section->maxTime = msa;
}
#endif
}
void printProfiler() {
#ifdef ENABLE_PROFILER
if (profiler_sections == NULL) return;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
double now = (double) ts.tv_nsec / 1000000. + (double) ts.tv_sec * 1000.;
#ifdef ENABLE_PROFILER_MT
pthread_rwlock_rdlock(&profiler_sections->rwlock);
#endif
ITER_MAP (profiler_sections) {
struct profiler_section* ps = value;
if (ps->sectionLastStart > 0.) {
ps->ms_spent += now - ps->sectionLastStart;
ps->sectionLastStart = -1.;
}
printf("%s: %f ms - %f ms/tick - %f maximum\n", ps->name, ps->ms_spent, ps->ms_spent / (now - ps->creation) * 50., ps->maxTime);
ITER_MAP_END()
}
#ifdef ENABLE_PROFILER_MT
pthread_rwlock_unlock(&profiler_sections->rwlock);
#endif
#endif
}
void clearProfiler() {
#ifdef ENABLE_PROFILER
#ifdef ENABLE_PROFILER_MT
pthread_rwlock_rdlock(&profiler_sections->rwlock);
#endif
ITER_MAP (profiler_sections) {
struct profiler_section* ps = value;
ps->ms_spent = 0.;
ps->maxTime = 0.;
ITER_MAP_END()
}
#ifdef ENABLE_PROFILER_MT
pthread_rwlock_unlock(&profiler_sections->rwlock);
#endif
#endif
}

View File

@ -5,14 +5,12 @@
* Author: root
*/
#include "smelting.h"
#include "item.h"
#include "collection.h"
#include "json.h"
#include <basin/smelting.h>
#include <basin/item.h>
#include <avuna/json.h>
#include <avuna/string.h>
#include <fcntl.h>
#include "xstring.h"
#include <errno.h>
#include "util.h"
#include <unistd.h>
struct collection* smelting_fuels;
@ -68,22 +66,22 @@ void init_smelting() {
}
close(fd);
struct json_object json;
parseJSON(&json, jsf);
struct json_object* fuels = getJSONValue(&json, "fuels");
json_parse(&json, jsf);
struct json_object* fuels = json_get(&json, "fuels");
if (fuels == NULL || fuels->type != JSON_OBJECT) goto cerr2;
struct json_object* recipes = getJSONValue(&json, "recipes");
struct json_object* recipes = json_get(&json, "recipes");
if (recipes == NULL || recipes->type != JSON_OBJECT) goto cerr2;
for (size_t i = 0; i < fuels->child_count; i++) {
struct json_object* ur = fuels->children[i];
struct smelting_fuel* ii = xcalloc(sizeof(struct smelting_fuel));
struct json_object* tmp = getJSONValue(ur, "id");
struct json_object* tmp = json_get(ur, "id");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->id = (int16_t) tmp->data.number;
if (ii->id <= 0) goto cerr;
tmp = getJSONValue(ur, "damage");
tmp = json_get(ur, "damage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->damage = (int16_t) tmp->data.number;
tmp = getJSONValue(ur, "burnTime");
tmp = json_get(ur, "burnTime");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr;
ii->burnTime = (int16_t) tmp->data.number;
add_smelting_fuel(ii);
@ -94,23 +92,23 @@ void init_smelting() {
for (size_t i = 0; i < recipes->child_count; i++) {
struct json_object* ur = recipes->children[i];
struct smelting_recipe* ii = xcalloc(sizeof(struct smelting_recipe));
struct json_object* tmp = getJSONValue(ur, "input_item");
struct json_object* tmp = json_get(ur, "input_item");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr3;
ii->input.item = (int16_t) tmp->data.number;
if (ii->input.item <= 0) goto cerr3;
tmp = getJSONValue(ur, "input_damage");
tmp = json_get(ur, "input_damage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr3;
ii->input.damage = (int16_t) tmp->data.number;
ii->input.itemCount = 1;
ii->input.nbt = NULL;
tmp = getJSONValue(ur, "output_item");
tmp = json_get(ur, "output_item");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr3;
ii->output.item = (int16_t) tmp->data.number;
if (ii->output.item <= 0) goto cerr3;
tmp = getJSONValue(ur, "output_damage");
tmp = json_get(ur, "output_damage");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr3;
ii->output.damage = (int16_t) tmp->data.number;
tmp = getJSONValue(ur, "output_count");
tmp = json_get(ur, "output_count");
if (tmp == NULL || tmp->type != JSON_NUMBER) goto cerr3;
ii->output.itemCount = (uint8_t) tmp->data.number;
ii->output.nbt = NULL;

View File

@ -5,15 +5,15 @@
* Author: root
*/
#include "nbt.h"
#include <basin/nbt.h>
#include <basin/network.h>
#include <basin/block.h>
#include <basin/inventory.h>
#include <basin/tileentity.h>
#include <basin/world.h>
#include <avuna/string.h>
#include <avuna/pmem.h>
#include <stdlib.h>
#include "util.h"
#include "xstring.h"
#include "network.h"
#include "block.h"
#include "inventory.h"
#include "tileentity.h"
#include "world.h"
void tetick_furnace(struct world* world, struct tile_entity* te) {
update_furnace(world, te);

105
src/tools.c Normal file
View File

@ -0,0 +1,105 @@
/*
* craft.c
*
* Created on: Dec 26, 2016
* Author: root
*/
#include <basin/globals.h>
#include <basin/network.h>
#include <basin/tools.h>
#include <basin/block.h>
#include <basin/item.h>
#include <basin/player.h>
#include <avuna/json.h>
#include <avuna/string.h>
#include <avuna/util.h>
#include <avuna/llist.h>
#include <avuna/log.h>
#include <fcntl.h>
#include <errno.h>
#include "util.h"
#include <unistd.h>
struct mempool* tool_pool;
struct hashmap* tool_infos;
struct tool_info* tools_get(char* name) {
size_t name_len = strlen(name) + 1;
char name_lowered[name_len];
memcpy(name_lowered, name, name_len);
str_tolower(name_lowered);
return hashmap_get(tool_infos, name_lowered);
}
int tools_proficient(struct tool_info* info, uint8_t harvest_level, block b) {
if (harvest_level < 0 || info == NULL) return 0;
if (harvest_level > info->proficient_size) harvest_level = (uint8_t) (info->proficient_size - 1);
for (size_t x = 0; x <= harvest_level; x++) {
for (size_t z = 0; z < info->proficiencies[x].proficient_size; z++) {
struct tool_proficiency* proficiency = &info->proficiencies[x];
if (proficiency == NULL) continue;
for (size_t i = 0; i < proficiency->proficient_size; i++) {
if ((proficiency->proficient[i] >> 4) == (b >> 4)) return 1;
}
}
}
return 0;
}
void tools_init() {
tool_pool = mempool_new();
char* json_file = (char*) read_file_fully(tool_pool, "tools.json", NULL);
if (json_file == NULL) {
errlog(delog, "Error reading tool data: %s\n", strerror(errno));
return;
}
struct json_object* json = NULL;
json_parse(tool_pool, &json, json_file);
pprefree(tool_pool, json_file);
if (json == NULL) {
errlog(delog, "Syntax error in 'tools.json'");
return;
}
if (json->type != JSON_OBJECT) {
errlog(delog, "Format error in 'tools.json'");
return;
}
ITER_LLIST(json->children_list, value) {
struct json_object* child = value;
if (child->type != JSON_OBJECT) {
goto format_error;
}
struct tool_info* info = pcalloc(tool_pool, sizeof(struct tool_info));
info->name = str_tolower(pxfer(json->pool, tool_pool, child->name));
info->proficiencies = pmalloc(tool_pool, sizeof(struct tool_proficiency) * child->children_list->size);
info->proficient_size = child->children_list->size;
size_t proficiency_index = 0;
ITER_LLIST(child->children_list, sub_value) {
struct json_object* sub_child = sub_value;
if (sub_child == NULL || sub_child->type != JSON_ARRAY) goto format_error;
info->proficiencies[proficiency_index].proficient = pmalloc(tool_pool, sizeof(block) * sub_child->children_list->size);
info->proficiencies[proficiency_index].proficient_size = sub_child->children_list->size;
size_t entry_index = 0;
ITER_LLIST(sub_child->children_list, sub_value2) {
struct json_object* sub_child2 = sub_value2;
if (sub_child2 == NULL || sub_child2->type != JSON_NUMBER) goto format_error;
info->proficiencies[proficiency_index].proficient[entry_index] = (block) sub_child2->data.number;
++entry_index;
ITER_LLIST_END();
}
++proficiency_index;
ITER_LLIST_END();
}
tools_add(info);
continue;
format_error: ;
errlog(delog, "[WARNING] Error Loading Tool \"%s\"! Skipped.\n", child->name);
ITER_LLIST_END();
}
pfree(json->pool);
}
void tools_add(struct tool_info* tool) {
hashmap_put(tool_infos, tool->name, tool);
}

View File

@ -14,29 +14,29 @@
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include "collection.h"
#include <basin/collection.h>
#include "util.h"
#include "streams.h"
#include <sys/ioctl.h>
#include "network.h"
#include "basin/network.h"
#include "packet.h"
#include "globals.h"
#include <basin/globals.h>
#include <openssl/md5.h>
#include "entity.h"
#include "server.h"
#include "worldmanager.h"
#include "queue.h"
#include "game.h"
#include "block.h"
#include <basin/entity.h>
#include <basin/server.h>
#include <basin/worldmanager.h>
#include <basin/queue.h>
#include <basin/game.h>
#include <basin/block.h>
#include <math.h>
#include "item.h"
#include <basin/item.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/sha.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include "version.h"
#include "json.h"
#include <basin/version.h>
#include <basin/json.h>
void closeConn(struct work_param* param, struct conn* conn) {
close(conn->fd);
@ -369,14 +369,14 @@ int handleRead(struct conn* conn, struct work_param* param, int fd) {
if (data == NULL) goto merr;
data += 4;
struct json_object json;
parseJSON(&json, data);
struct json_object* tmp = getJSONValue(&json, "id");
json_parse(&json, data);
struct json_object* tmp = json_get(&json, "id");
if (tmp == NULL || tmp->type != JSON_STRING) {
freeJSON(&json);
goto merr;
}
char* id = trim(tmp->data.string);
tmp = getJSONValue(&json, "name");
tmp = json_get(&json, "name");
if (tmp == NULL || tmp->type != JSON_STRING) {
freeJSON(&json);
goto merr;

View File

@ -8,9 +8,9 @@
#ifndef WORK_H_
#define WORK_H_
#include "collection.h"
#include <basin/collection.h>
#include "accept.h"
#include "log.h"
#include <basin/log.h>
struct work_param {
struct collection* conns;

View File

@ -5,43 +5,41 @@
* Author: root
*/
#include "hashmap.h"
#include "entity.h"
#include "basin/network.h"
#include "packet.h"
#include <basin/game.h>
#include <basin/block.h>
#include <basin/queue.h>
#include <basin/tileentity.h>
#include <basin/entity.h>
#include <basin/world.h>
#include <basin/globals.h>
#include <basin/profile.h>
#include <basin/server.h>
#include <basin/ai.h>
#include <basin/plugin.h>
#include <basin/perlin.h>
#include <basin/biome.h>
#include <basin/entity.h>
#include <basin/player.h>
#include <basin/nbt.h>
#include <basin/worldmanager.h>
#include <avuna/prqueue.h>
#include <avuna/string.h>
#include <avuna/pmem.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "collection.h"
#include <pthread.h>
#include "player.h"
#include "nbt.h"
#include <linux/limits.h>
#include <fcntl.h>
#include <stdio.h>
#include "util.h"
#include <unistd.h>
#include <sys/mman.h>
#include <zlib.h>
#include <dirent.h>
#include "worldmanager.h"
#include "network.h"
#include "packet.h"
#include "game.h"
#include "block.h"
#include <math.h>
#include "queue.h"
#include "tileentity.h"
#include "xstring.h"
#include "entity.h"
#include "world.h"
#include "globals.h"
#include "profile.h"
#include <errno.h>
#include "server.h"
#include "ai.h"
#include "plugin.h"
#include "perlin.h"
#include "biome.h"
#include "prqueue.h"
int boundingbox_intersects(struct boundingbox* bb1, struct boundingbox* bb2) {
return (((bb1->minX >= bb2->minX && bb1->minX <= bb2->maxX) || (bb1->maxX >= bb2->minX && bb1->maxX <= bb2->maxX) || (bb2->minX >= bb1->minX && bb2->minX <= bb1->maxX) || (bb2->maxX >= bb1->minX && bb2->maxX <= bb1->maxX)) && ((bb1->minY >= bb2->minY && bb1->minY <= bb2->maxY) || (bb1->maxY >= bb2->minY && bb1->maxY <= bb2->maxY) || (bb2->minY >= bb1->minY && bb2->minY <= bb1->maxY) || (bb2->maxY >= bb1->minY && bb2->maxY <= bb1->maxY)) && ((bb1->minZ >= bb2->minZ && bb1->minZ <= bb2->maxZ) || (bb1->maxZ >= bb2->minZ && bb1->maxZ <= bb2->maxZ) || (bb2->minZ >= bb1->minZ && bb2->minZ <= bb1->maxZ) || (bb2->maxZ >= bb1->minZ && bb2->maxZ <= bb1->maxZ)));

View File

@ -5,8 +5,8 @@
* Author: root
*/
#include "world.h"
#include "worldmanager.h"
#include <basin/world.h>
#include <basin/worldmanager.h>
struct world* getWorldByID(int32_t id) {
for (size_t i = 0; i < worlds->size; i++) {