Skip to content

Commit

Permalink
Merge branch 'release-1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaakman committed Aug 19, 2015
2 parents 5a054df + 7bd54ee commit 176bbc1
Show file tree
Hide file tree
Showing 31 changed files with 938 additions and 691 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ obj/%.o: src/%.cpp
mkdir -p $(@D)
gcc -std=c++0x -D DEBUG -c $< -o $@ $(INCDIRS:%=-I%)

bin/server: obj/geo2d.o obj/str.o obj/ini.o obj/account.o obj/server/server.o
bin/server: obj/geo2d.o obj/str.o obj/ini.o obj/account.o obj/server/server.o obj/err.o
gcc $^ -o $@ -lstdc++ $(SERVERLIBS:%=-l%) $(LIBDIRS:%=-L%)

bin/client: obj/geo2d.o obj/ini.o obj/client/client.o obj/util.o obj/client/connection.o obj/str.o obj/err.o obj/client/textscroll.o\
Expand All @@ -27,5 +27,5 @@ bin/test3d: obj/test3d/water.o obj/ini.o obj/test3d/hub.o obj/xml.o obj/str.o ob
obj/io.o obj/texture.o obj/test3d/mesh.o obj/util.o obj/font.o obj/test3d/collision.o obj/test3d/toon.o
gcc $^ -o $@ $(TEST3DLIBS:%=-l%) $(LIBDIRS:%=-L%)

bin/manager: obj/manager/manager.o obj/str.o obj/account.o
bin/manager: obj/manager/manager.o obj/str.o obj/account.o obj/err.o
gcc $^ -o $@ -lstdc++ $(MANAGERLIBS:%=-l%) $(LIBDIRS:%=-L%)
Binary file modified bin/test3d.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions manager.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
</Linker>
<Unit filename="src/account.cpp" />
<Unit filename="src/account.h" />
<Unit filename="src/err.cpp" />
<Unit filename="src/err.h" />
<Unit filename="src/manager/manager.cpp" />
<Unit filename="src/str.cpp" />
<Unit filename="src/str.h">
Expand Down
2 changes: 1 addition & 1 deletion notes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Game templates version 1.2.0
Game templates version 1.3.0

[test3d]

Expand Down
2 changes: 2 additions & 0 deletions server.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
</Linker>
<Unit filename="src/account.cpp" />
<Unit filename="src/account.h" />
<Unit filename="src/err.cpp" />
<Unit filename="src/err.h" />
<Unit filename="src/geo2d.cpp" />
<Unit filename="src/geo2d.h" />
<Unit filename="src/ini.cpp" />
Expand Down
113 changes: 71 additions & 42 deletions src/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@

#include "account.h"
#include "str.h"
#include "err.h"
#include <errno.h>

#define HASHSTRING_LENGTH 20
void getHash(const char* username, const char* password, unsigned char* hash)
{
/*
This function is used for checking passwords.
If it changes, all account files must be created anew.
*/

SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx,(void*)username,strlen(username));
Expand All @@ -41,14 +47,9 @@ void getHash(const char* username, const char* password, unsigned char* hash)

void AccountFileName (const char *directory, const char *username, char *out)
{
sprintf(out,"%s%c%s.account", directory, PATH_SEPARATOR, username);
}
// <username>.account

std::string accountError;

const char *GetAccountError ()
{
return accountError.c_str();
sprintf(out,"%s%c%s.account", directory, PATH_SEPARATOR, username);
}

#define LINEID_ACCOUNT "ACCOUNT"
Expand All @@ -59,30 +60,40 @@ bool MakeAccount (const char* dirPath, const char* username, const char* passwor
_password[PASSWORD_MAXLENGTH];
unsigned char hash[HASHSTRING_LENGTH];

/*
username is case insensitive, thus always converted to lowercase
password is case sensitive
*/
int i;
for(i=0;username[i];i++)
for (i=0;username[i];i++)
_username[i]=tolower(username[i]);
_username[i]=NULL;
for(i=0;password[i];i++)
_password[i]=tolower(password[i]);
_password[i]=NULL;

strcpy (_password, password);

// Open the account file
AccountFileName (dirPath, _username, filepath);

FILE* f=fopen (filepath, "wb");
if(!f)
FILE* f = fopen (filepath, "wb");
if (!f)
{
accountError = std::string("cannot open output file ") + filepath + ": " + strerror(errno);
std::string _errorstring = std::string ("cannot open output file ") + filepath + ": " + strerror (errno);
SetError (_errorstring.c_str ());
return false;
}

getHash(_username, _password, hash);
fprintf(f,"%s %s ",LINEID_ACCOUNT,_username);
/*
Write a single line, including
the username and password hash.
*/

getHash (_username, _password, hash);
fprintf (f, "%s %s ", LINEID_ACCOUNT, _username);

// write each hash byte
for(i=0; i<HASHSTRING_LENGTH; i++)
for (i=0; i<HASHSTRING_LENGTH; i++)
{
fprintf(f,"%.2X",hash[i]);
fprintf (f, "%.2X", hash[i]);
}

fclose(f);
Expand All @@ -91,16 +102,22 @@ bool MakeAccount (const char* dirPath, const char* username, const char* passwor
}
void delAccount(const char* dirPath, const char* username)
{
/*
username is case insensitive, thus always converted to lowercase
*/

char filepath[FILENAME_MAX],
_username[USERNAME_MAXLENGTH];
int i;
for(i=0;username[i];i++)
_username[i]=tolower(username[i]);
_username[i]=NULL;

// Remove the file, associated with this username

AccountFileName (dirPath, _username, filepath);

remove(filepath);
remove (filepath);
}
bool authenticate(const char* dirPath, const char* username, const char* password)
{
Expand All @@ -113,57 +130,69 @@ bool authenticate(const char* dirPath, const char* username, const char* passwor
unsigned char b;
unsigned char hash [HASHSTRING_LENGTH];

/*
username is case insensitive, thus always converted to lowercase
password is case sensitive
*/
int i;

for(i=0;username[i] && i<(USERNAME_MAXLENGTH-1);i++)
_username[i]=tolower(username[i]);
for (i = 0; username[i] && i < (USERNAME_MAXLENGTH - 1); i++)
_username[i] = tolower (username[i]);
_username[i]=NULL;

for(i=0;password[i] && i<(PASSWORD_MAXLENGTH-1);i++)
_password[i]=tolower(password[i]);
_password[i]=NULL;
strcpy (_password, password);

getHash(_username, _password, hash);
getHash (_username, _password, hash);

AccountFileName (dirPath, _username, filepath);

FILE* f=fopen(filepath,"rb");
if(!f) // the user probably doesn't exist
FILE* f = fopen (filepath, "rb");
if (!f) // the user probably doesn't exist
{
return false;
}

while(!feof(f))
while (!feof(f))
{
fgets(line,lineLength,f);
if(strncmp(LINEID_ACCOUNT,line,strlen(LINEID_ACCOUNT))==0)
fgets (line, lineLength, f);

// Check for the recognition bytes at the beginning of the file:
if (strncmp (LINEID_ACCOUNT, line, strlen (LINEID_ACCOUNT)) == 0)
{
int i=0,n,j;
while(line[i] && !isspace(line[i])) i++;
while(line[i] && isspace(line[i])) i++;
n=i;
while(line[n] && !isspace(line[n])) n++;
int i = 0, n, j;

// Read past the recognition bytes and the spaces after it.
while (line[i] && !isspace (line[i])) i++;
while (line[i] && isspace (line[i])) i++;

if(n==i)
// Read all the non-whitespace characters, presumed to be username
n = i;
while (line[n] && !isspace (line[n])) n++;

if (n == i) // no more non-whitespace characters found
return false;

if(strncmp(_username,line + i, n - i) != 0)
// Verify the username in the file:
if (strncmp (_username, line + i, n - i) != 0)
return false;

i=n;
while(line[i] && isspace(line[i])) i++;
// Read the spaces past the username
i = n;
while (line[i] && isspace (line [i])) i++;

// compare each hash byte:
for(j=0;j<HASHSTRING_LENGTH;j++)
for (j = 0; j < HASHSTRING_LENGTH; j++)
{
if(sscanf(line + i + j * 2, "%2X", &b) != 1)
// Read in the hash byte:
if (sscanf(line + i + j * 2, "%2X", &b) != 1)
{
fprintf (stderr, "%s: cannot read byte %d of hash\n", filepath, j);
return false;
}

if(b != hash[j])
{
// mismatch in hash

return false;
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@
#define USERNAME_MAXLENGTH 14
#define PASSWORD_MAXLENGTH 18

/*
For using these functions in an application,
it's necessary that dirPath points to a valid
directory where account files are to be stored.
*/

/**
* :returns: true on success, false otherwise.
*/
bool MakeAccount (const char* dirPath, const char* username, const char* password);
void delAccount (const char* dirPath, const char* username);
bool authenticate (const char* dirPath, const char* username, const char* password);

const char *GetAccountError ();

#endif // ACCOUNT_H
18 changes: 10 additions & 8 deletions src/err.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@
#include <cstdio>
#include <cstring>

#define ERRSTR_LEN 512
#define ERRSTR_LEN 16384

// String to store the error in:
char error [ERRSTR_LEN] = "";
char error [ERRSTR_LEN] = "",
tmp [ERRSTR_LEN];

const char *GetError ()
{
return error;
}
void SetError (const char* format, ...)
{
// use a temporary buffer, because 'error' might be one of the args
char tmp [ERRSTR_LEN];

// Insert args:
va_list args; va_start (args, format);

vsprintf (tmp, format, args);
// use the temporary buffer, because 'error' might be one of the args
int res = vsprintf (tmp, format, args);

va_end (args);

// copy from temporary buffer:
strcpy (error, tmp);
if (res > 0)
{
// copy from temporary buffer:
strcpy (error, tmp);
}
}
51 changes: 30 additions & 21 deletions src/geo2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/


#include"geo2d.h"
#include<math.h>
#include "geo2d.h"
#include <math.h>

vec2::vec2(){}
vec2::vec2(float _x, float _y)
Expand Down Expand Up @@ -80,56 +80,65 @@ vec2 vec2::unit() const
vec2 vec2::rotate(float a) const
{
vec2 r;
const float c=cos(a),
s=sin(a);
const float
c = cos(a),
s = sin(a);

r.x=c*x - s*y;
r.y=s*x + c*y;
r.x = c * x - s * y;
r.y = s * x + c * y;

return r;
}
float vec2::angle() const
float vec2::angle () const
{
return atan(y/x);
return atan (y / x);
}
vec2 operator* (const float& f, const vec2& v)
{
return (v*f);
return (v * f);
}
float distance2(const vec2& v1, const vec2& v2)
{
return (v2-v1).length2();
return (v2 - v1).length2();
}
float distance(const vec2& v1, const vec2& v2)
{
return (v2-v1).length();
return (v2 - v1).length();
}
float dot (const vec2& v1, const vec2& v2)
{
return v1.x*v2.x+v1.y*v2.y;
return v1.x * v2.x + v1.y * v2.y;
}
float angle (const vec2&v1,const vec2&v2)
{
float a=acosf(dot(v1.unit(),v2.unit()));
while(a>M_PI) { a-=2*M_PI; }
while(a<=-M_PI) { a+=2*M_PI; }

/*
Return an angle between -PI and PI.
Adding or subtracting 2PI always results
in the same angle.
*/
while (a > M_PI) { a -= 2*M_PI; }
while (a <= -M_PI) { a += 2*M_PI; }

return a;
}
vec2 projection(const vec2& v,const vec2& on_v)
vec2 projection (const vec2& v,const vec2& on_v)
{
return on_v*dot(v,on_v)/on_v.length2();
return on_v * dot (v, on_v) / on_v.length2 ();
}
vec2 lineIntersection(const vec2& a1, const vec2& a2, const vec2& b1, const vec2& b2)
{
float d=(a1.x-a2.x)*(b1.y-b2.y)-(a1.y-a2.y)*(b1.x-b2.x),
a=a1.x*a2.y-a1.y*a2.x,
b=b1.x*b2.y-b1.y*b2.x;
vec2 r((a*(b1.x-b2.x)-b*(a1.x-a2.x))/d,(a*(b1.y-b2.y)-b*(a1.y-a2.y))/d);
float d = (a1.x - a2.x) * (b1.y - b2.y) - (a1.y - a2.y) * (b1.x - b2.x),
a = a1.x * a2.y - a1.y * a2.x,
b = b1.x * b2.y - b1.y * b2.x;

vec2 r ((a*(b1.x-b2.x)-b*(a1.x-a2.x))/d,(a*(b1.y-b2.y)-b*(a1.y-a2.y))/d);
return r;
}
vec2 pointOnBezierCurve(float ti, const vec2& p0, const vec2& p1, const vec2& p2, const vec2& p3)
{
float invti = 1.0f-ti;
float invti = 1.0f - ti;

return invti*invti*invti*p0 + 3*invti*invti*ti*p1 + 3*invti*ti*ti*p2 + ti*ti*ti*p3;
}
Loading

0 comments on commit 176bbc1

Please sign in to comment.