Jump to content

LEPlugin No longer working.


reepblue
 Share

Recommended Posts

I was dumb enough to delete a source file for a texture and today I decided to write an app to decompile the tex file to a TGA. The old Plugin DLL file didn't work, so I pulled the PluginSDK, recompiled the plugin myself and it compiled with no issues after one commented line.

Now, I'm getting an exception thrown at on the first MemReader::Read call.

Here's a copy of my modded main file.

#include "../SDK/MemWriter.h"
#include "DLLExports.h"
#include "VKFormat.h"
#include <algorithm>

using namespace GMFSDK;
using namespace std;

MemWriter* writer = nullptr;
std::vector<void*> allocedmem;

//DLL entry point function
#ifdef _WIN32
BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved)
{
	switch (fdwReason)
	{
	case DLL_PROCESS_ATTACH:
		writer = nullptr;
		break;
	case DLL_PROCESS_DETACH:
		Cleanup();
		break;
	}
	return TRUE;
}
#endif

#ifdef __APPLE__
// Initializer.
__attribute__((constructor))
static void initializer(void) {
	FreeImage_Initialise();
}

// Finalizer.
__attribute__((destructor))
static void finalizer(void) {
	FreeImage_DeInitialise();
}
#endif

//Returns all plugin information in a JSON string
int GetPluginInfo(unsigned char* cs, int maxsize)
{
	std::string s =
		"{"
		"\"plugin\":{"
		"\"title\":\"FreeImage texture loader.\","
		"\"description\":\"Load textures from common image file formats.\","
		"\"author\":\"Josh Klint\","
		"\"threadSafe\":true,"
		"\"url\":\"www.turboengine.com\","
		"\"extension\": [\"bmp\",\"jpg\",\"jpeg\",\"tga\",\"pcx\",\"tga\",\"gif\"],"
		"\"saveextensions\": [\"bmp\",\"jpg\",\"jpeg\",\"tga\",\"tga\"],"
		"\"filter\": [\"Portable Network Graphics (*.png):png\",\"Windows Bitmap (*bmp):bmp\"]"
		"}"
		"}";
#ifndef _WIN64
	if (maxsize > 0) memcpy(cs, s.c_str(), min(maxsize, s.length()));
#else
	if (maxsize > 0) memcpy(cs, s.c_str(), fmin(maxsize, s.length()));
#endif
	return s.length();
}

void Cleanup()
{
	delete writer;
	writer = nullptr;
	for (auto levelData : allocedmem)
	{
		free(levelData);
	}
	allocedmem.clear();
}

const int LE_TEXTURE_RGBA8 = 1;
const int LE_TEXTURE_RGB8 = 7;
const int LE_TEXTURE_RGBADXTC1 = 4;
const int LE_TEXTURE_RGBDXTC1 = 8;
const int LE_TEXTURE_RGBADXTC3 = 5;
const int LE_TEXTURE_RGBADXTC5 = 6;
const int LE_TEXTURE_RGBADXT5N = 20;

const int LE_TEXTURE_1D = 1;
const int LE_TEXTURE_2D = 2;
const int LE_TEXTURE_3D = 3;
const int LE_TEXTURE_CUBEMAP = 4;

void* LoadTexture(void* data, uint64_t size, wchar_t* cpath, uint64_t& returnsize)
{
	MemReader reader(data, size);
	int tag;
	reader.Read(&tag,4);
	if (tag != 5784916) return nullptr; // "TEX"

	int version;
	reader.Read(&version);
	if (version != 1) return nullptr;

	TextureInfo texinfo;

	int format;
	reader.Read(&format);

	switch (format)
	{
	case LE_TEXTURE_RGB8:
		texinfo.format = VK_FORMAT_R8G8B8_UNORM;
		break;
	case LE_TEXTURE_RGBA8:
		texinfo.format = VK_FORMAT_R8G8B8A8_UNORM;
		break;
	case LE_TEXTURE_RGBDXTC1:
		texinfo.format = VK_FORMAT_BC1_RGB_UNORM_BLOCK;
		break;
	case LE_TEXTURE_RGBADXTC1:
		texinfo.format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
		break;
	case LE_TEXTURE_RGBADXTC3:
		texinfo.format = VK_FORMAT_BC2_UNORM_BLOCK;
		break;
	//case LE_TEXTURE_RGBADXT5N:
	//	texinfo.swizzle_red_alpha = 1;
	case LE_TEXTURE_RGBADXTC5:
		texinfo.format = VK_FORMAT_BC3_UNORM_BLOCK;
		break;
	default:
		printf("Unknown texture format.");
		return nullptr;
		break;
	}

	int target;
	reader.Read(&target);

	if (target == LE_TEXTURE_1D)
	{
		texinfo.target = 1;
	}
	else if (target == LE_TEXTURE_2D)
	{
		texinfo.target = 2;
	}
	else if (target == LE_TEXTURE_3D)
	{
		texinfo.target = 3;
	}
	else if (target == LE_TEXTURE_CUBEMAP)
	{
		texinfo.target = 4;
		texinfo.faces = 6;
	}
	else
	{
		printf("Unknown texture target.");
		return nullptr;
	}

	reader.Read(&texinfo.width);
	reader.Read(&texinfo.height);
	reader.Read(&texinfo.depth);

	reader.Read(&texinfo.filter);
	reader.Read(&texinfo.clampu);
	reader.Read(&texinfo.clampv);
	reader.Read(&texinfo.clampw);

	reader.Read(&texinfo.frames);
	reader.Read(&texinfo.mipmaps);

	writer = new MemWriter();
	writer->Write(&texinfo);

	int mw, mh, sz;
	for (int i = 0; i < texinfo.mipmaps; i++)
	{
		reader.Read(&mw);
		reader.Read(&mh);
		reader.Read(&sz);
		for (int f = 0; f < texinfo.faces; ++f)
		{
			void* memblock = malloc(sz);
			if (memblock == nullptr)
			{
				printf("Error: Failed to allocate memory of size %i.\n", sz);
				return nullptr;
			}
			allocedmem.push_back(memblock);
			reader.Read(memblock, sz);
			writer->Write(&memblock, sizeof(void*));
		}
	}

	returnsize = writer->Size();
	return writer->data();
}

 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...