import rev0 (other stuff)

parent f4459488
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <ogc/machine/processor.h>
#include <wiiuse/wpad.h>
#include <vector>
#include <string>
#include <sys/dir.h>
#include "main.h"
#include "dolloader.h"
#include "elfloader.h"
#include "filelist.h"
#include "DiskOperations/di2.h"
#include "Tools/load_channel.h"
#include "Tools/parser.h"
#define BLOCKSIZE 70*1024 //70KB
#define MAX_CMDLINE 4096
#define MAX_ARGV 1000
#define GC_DOL_MAGIC "gchomebrew dol"
#define GC_MAGIC_BUF (char *) 0x807FFFE0
#define GC_DOL_BUF (u8 *)0x80800000
#define BC 0x0000000100000100ULL
struct __argv args;
char cmdline[MAX_CMDLINE];
char *a_argv[MAX_ARGV];
char *meta_buf = NULL;
static u8 *homebrewbuffer = (u8 *) 0x92000000;
static u32 homebrewsize = 0;
std::string filepath;
void arg_init()
{
memset(&args, 0, sizeof(args));
memset(cmdline, 0, sizeof(cmdline));
memset(a_argv, 0, sizeof(a_argv));
args.argvMagic = ARGV_MAGIC;
args.length = 1; // double \0\0
args.argc = 0;
args.commandLine = cmdline;
args.argv = a_argv;
args.endARGV = a_argv;
}
char* strcopy(char *dest, const char *src, int size)
{
strncpy(dest,src,size);
dest[size-1] = 0;
return dest;
}
int arg_add(const char *arg)
{
int len = strlen(arg);
if (args.argc >= MAX_ARGV) return -1;
if (args.length + len + 1 > MAX_CMDLINE) return -1;
strcopy(cmdline + args.length - 1, arg, len+1);
args.length += len + 1; // 0 term.
cmdline[args.length - 1] = 0; // double \0\0
args.argc++;
args.endARGV = args.argv + args.argc;
return 0;
}
/****************************************************************************
* FileSize
*
* Get filesize in bytes. u64 for files bigger than 4GB
***************************************************************************/
u64 FileSize(const char * filepath)
{
struct stat filestat;
if (stat(filepath, &filestat) != 0)
return 0;
return filestat.st_size;
}
/****************************************************************************
* LoadFileToMem
*
* Load up the file into a block of memory
***************************************************************************/
int LoadFileToMem(const char *filepath, u8 **inbuffer, u64 *size)
{
int ret = -1;
u64 filesize = FileSize(filepath);
char * filename = strrchr(filepath, '/');
if(filename)
filename++;
*inbuffer = NULL;
*size = 0;
FILE *file = fopen(filepath, "rb");
if (file == NULL)
return -1;
u8 *buffer = (u8 *) malloc(filesize);
if (buffer == NULL)
{
fclose(file);
return -2;
}
u64 done = 0;
u32 blocksize = BLOCKSIZE;
do
{
if(blocksize > filesize-done)
blocksize = filesize-done;
ret = fread(buffer+done, 1, blocksize, file);
if(ret < 0)
{
free(buffer);
fclose(file);
return -3;
}
else if(ret == 0)
{
//we are done
break;
}
done += ret;
}
while(done < filesize);
fclose(file);
if (done != filesize)
{
free(buffer);
return -3;
}
*inbuffer = buffer;
*size = filesize;
return 1;
}
int CopyHomebrewMemory(u8 *temp, u32 pos, u32 len)
{
homebrewsize += len;
memcpy((homebrewbuffer)+pos, temp, len);
return 1;
}
int LoadHomebrew(const char * path)
{
filepath = path;
u8 *buffer = NULL;
u64 filesize = 0;
int ret = LoadFileToMem(path, &buffer, &filesize);
if(ret < 0)
return ret;
ret = CopyHomebrewMemory(buffer, 0, filesize);
if(buffer) {
free(buffer);
buffer = NULL;
}
return ret;
}
int BootHomebrew()
{
if(homebrewsize == 0)
return -1;
entrypoint entry;
u32 cpu_isr;
arg_init();
arg_add(filepath.c_str()); // argv[0] = filepath
while(parser(Settings.forwarder_arg, "<arg>", "</arg>") != "")
{
arg_add(parser(Settings.forwarder_arg, "<arg>", "</arg>").c_str());
Settings.forwarder_arg.erase(0, Settings.forwarder_arg.find("</arg>") +1);
}
if ( valid_elf_image(homebrewbuffer) == 1 )
entry = (entrypoint) load_elf_image(homebrewbuffer);
else
entry = (entrypoint) load_dol(homebrewbuffer, &args);
if (!entry)
return -1;
SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
_CPU_ISR_Disable (cpu_isr);
__exception_closeall();
entry();
_CPU_ISR_Restore (cpu_isr);
return 0;
}
int BootGameCubeHomebrew()
{
if(homebrewsize == 0)
return -1;
static tikview view ATTRIBUTE_ALIGN(32);
DI2_Init();
DI2_Reset();
DI2_ReadDiscID((u64 *) 0x80000000);
DI2_Mount();
strcpy(GC_MAGIC_BUF, GC_DOL_MAGIC);
DCFlushRange(GC_MAGIC_BUF, 32);
memcpy(GC_DOL_BUF, homebrewbuffer, homebrewsize);
DCFlushRange(GC_DOL_BUF, homebrewsize);
*(vu32 *) 0xCC003024 |= 0x07;
ES_GetTicketViews(BC, &view, 1);
int ret = ES_LaunchTitle(BC, &view);
if(ret < 0)
LoadHBC();
return ret;
}
#ifndef _BOOTHOMEBREW_H_
#define _BOOTHOMEBREW_H_
int BootHomebrew();
int BootGameCubeHomebrew();
int CopyHomebrewMemory(u8 *temp, u32 pos, u32 len);
void AddBootArgument(const char * arg);
int LoadHomebrew(const char * filepath);
#endif
#include <dirent.h>
#include "FreeTypeGX.h"
#include "filelist.h"
#include "main.h"
#include "Tools/save.h"
FreeTypeGX * fontSystem = NULL;
static FT_Byte * MainFont = (FT_Byte *) font_ttf;
static u32 MainFontSize = font_ttf_size;
static float scale = 1;
void ClearFontData()
{
if(fontSystem)
delete fontSystem;
fontSystem = NULL;
if(MainFont != (FT_Byte *) font_ttf)
{
if(MainFont != NULL)
delete [] MainFont;
MainFont = (FT_Byte *) font_ttf;
MainFontSize = font_ttf_size;
}
}
bool SetupDefaultFont(const char *path)
{
bool result = false;
FILE *pfile = NULL;
ClearFontData();
if(path)
pfile = fopen(path, "rb");
if(pfile)
{
fseek(pfile, 0, SEEK_END);
MainFontSize = ftell(pfile);
rewind(pfile);
MainFont = new (std::nothrow) FT_Byte[MainFontSize];
if(!MainFont)
{
MainFont = (FT_Byte *) font_ttf;
MainFontSize = font_ttf_size;
}
else
{
fread(MainFont, 1, MainFontSize, pfile);
result = true;
}
fclose(pfile);
}
fontSystem = new FreeTypeGX(MainFont, MainFontSize);
return result;
}
void SetFontScale(float Scale)
{
scale = Scale;
}
float GetFontScale()
{
return scale;
}
void SetFont()
{
SetupDefaultFont((check_path(Settings.device_dat + ":/config/Homebrew Filter/fonts/") + Options.font).c_str());
SetFontScale( (float)51 / (float)fontSystem->getWidth(charToWideChar("a"), 100) ); // 51 -> breite von "a" mit standart schrift
}
bool font_folder_exists()
{
// font speicher ordner erstellen
if(folder_exists())
{
DIR *pDir;
pDir = opendir(check_path(Settings.device_dat + ":/config/Homebrew Filter/Fonts").c_str());
if(pDir != NULL)
{
closedir (pDir);
return true;
}
else
{
if (mkdir((Settings.device_dat + ":/config/Homebrew Filter/Fonts").c_str(), 0777) != -1)
{
closedir (pDir);
return true;
}
}
closedir (pDir);
}
return false;
}
\ No newline at end of file
#ifndef FONTSYSTEM_H_
#define FONTSYSTEM_H_
bool SetupDefaultFont(const char *path);
void ClearFontData();
void SetFontScale(float Scale);
float GetFontScale();
void SetFont();
bool font_folder_exists();
#endif
\ No newline at end of file
/***************************************************************************
* Copyright (C) 2009
* by Dimok & r-win
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
* Resources.cpp
*
* for WiiXplorer 2009
***************************************************************************/
#include "Resources.h"
Resources * Resources::instance = NULL;
Resources * Resources::Instance()
{
if (instance == NULL)
{
instance = new Resources();
}
return instance;
}
void Resources::DestroyInstance()
{
if (instance != NULL)
{
delete instance;
instance = NULL;
}
}
Resources::~Resources()
{
// Delete all images...
std::map<const u8 *, GuiImageData *>::iterator imgitr;
for (imgitr = images.begin(); imgitr != images.end(); imgitr++)
{
delete imgitr->second;
}
images.clear();
// Delete all sounds...
std::map<const u8 *, GuiSound *>::iterator snditr;
for (snditr = sounds.begin(); snditr != sounds.end(); snditr++)
{
delete snditr->second;
}
sounds.clear();
imageCount.clear();
soundCount.clear();
}
GuiImageData *Resources::GetImageData(const u8 *img, u32 imgSize)
{
return Resources::Instance()->InternalGetImageData(img, imgSize);
}
GuiSound *Resources::GetSound(const u8 *snd, s32 sndSize)
{
return Resources::Instance()->InternalGetSound(snd, sndSize);
}
void Resources::Remove(GuiImageData *img)
{
Resources::Instance()->InternalRemoveImageData(img);
}
void Resources::Remove(GuiSound *snd)
{
Resources::Instance()->InternalRemoveSound(snd);
}
GuiImageData *Resources::InternalGetImageData(const u8 *img, u32 imgSize)
{
std::map<const u8 *, GuiImageData *>::iterator itr = images.find(img);
if (itr == images.end())
{
// Not found, create a new one
GuiImageData *d = new GuiImageData(img);
images[img] = d;
imageCount[d] = 1;
return d;
}
imageCount[itr->second]++;
return itr->second;
}
GuiSound *Resources::InternalGetSound(const u8 *snd, s32 sndSize)
{
std::map<const u8 *, GuiSound *>::iterator itr = sounds.find(snd);
if (itr == sounds.end())
{
// Not found, create a new one
// Only unallocated sounds go here
GuiSound *d = new GuiSound(snd, sndSize, false);
sounds[snd] = d;
soundCount[d] = 1;
return d;
}
soundCount[itr->second]++;
return itr->second;
}
void Resources::InternalRemoveImageData(GuiImageData *img)
{
std::map<GuiImageData *, int>::iterator itr = imageCount.find(img);
if (itr != imageCount.end())
{
itr->second--;
if (itr->second == 0) // Remove the resource
{
imageCount.erase(itr);
std::map<const u8 *, GuiImageData *>::iterator iitr;
for (iitr = images.begin(); iitr != images.end(); iitr++)
{
if (iitr->second == img)
{
delete iitr->second;
images.erase(iitr);
break;
}
}
}
}
}
void Resources::InternalRemoveSound(GuiSound *snd)
{
std::map<GuiSound *, int>::iterator itr = soundCount.find(snd);
if (itr != soundCount.end())
{
itr->second--;
if (itr->second == 0) // Remove the resource
{
soundCount.erase(itr);
std::map<const u8 *, GuiSound *>::iterator iitr;
for (iitr = sounds.begin(); iitr != sounds.end(); iitr++)
{
if (iitr->second == snd)
{
delete iitr->second;
sounds.erase(iitr);
break;
}
}
}
}
}
/***************************************************************************
* Copyright (C) 2009
* by Dimok & r-win
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
* Resources.h
*
* for WiiXplorer 2009
***************************************************************************/
#ifndef _RESOURCES_H
#define _RESOURCES_H
#define UNUSED __attribute__((unused))
#include <gccore.h>
#include "libwiigui/gui.h"
#include "filelist.h"
#include <map>
class Resources
{
public:
static Resources *Instance();
static void DestroyInstance();
//! Only images/sounds that are compiled into the app go here
static GuiImageData *GetImageData(const u8 *img, u32 imgSize);
static GuiSound *GetSound(const u8 *snd, s32 sndSize);
static void Remove(GuiImageData *img);
static void Remove(GuiSound *snd);
private:
GuiImageData *InternalGetImageData(const u8 *img, u32 imgSize);
GuiSound *InternalGetSound(const u8 *snd, s32 sndSize);
void InternalRemoveImageData(GuiImageData *img);
void InternalRemoveSound(GuiSound *snd);
~Resources();
static Resources *instance;
std::map<const u8 *, GuiImageData *> images;
std::map<const u8 *, GuiSound *> sounds;
std::map<GuiImageData *, int> imageCount;
std::map<GuiSound *, int> soundCount;
};
#endif //_RESOURCES_H
#include "mem2.h"
#include "mem2alloc.hpp"
#include <malloc.h>
#include <string.h>