OpenTTDRandom Homebrew: OpenTTD
Clone of Transport Tycoon Duluxe on PC, Wilreless Multiplayer.
Friends: Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita

Display text in game from plugin

Forum rules
Any post not directly related to programming will be moderated.
Do not request people to code something for you.
Avoid posting messages that do not bring anything to the conversation. We want the threads in this subforum to stay focused.

Display text in game from plugin

Postby svanheulen » Thu Dec 23, 2010 6:37 pm

I would like to be able to print text to the screen while in a game from a plugin. The CWCheat plugin does it perfectly, pausing the game and everything. Is there any exmaple code out there for doing this or can anyone help me out?
svanheulen
 
Posts: 33
Joined: Thu Dec 23, 2010 6:30 pm

Re: Display text in game from plugin

Postby svanheulen » Sun Dec 26, 2010 12:38 am

Ok, I found some good info in this thread:
http://forums.psp-hacks.com/f141/pausin ... n-t228379/

The only problem with this is that it suspends threads that control things like file I/O, which I need. CWCheat does file I/O while the game is paused so I'm guessing it only pauses the game's threads... how would I go about decerning which threads belong to the game?
svanheulen
 
Posts: 33
Joined: Thu Dec 23, 2010 6:30 pm

Re: Display text in game from plugin

Postby svanheulen » Sun Dec 26, 2010 10:19 pm

Ok, I got something working but it's not really the best way of doing it I think...
Here's my main.c:
Code: Select all
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <pspctrl.h>
#include <pspthreadman_kernel.h>

PSP_MODULE_INFO("TEST", PSP_MODULE_KERNEL, 0, 1);
PSP_NO_CREATE_MAIN_THREAD();

int main_thread(SceSize argc, void* argp) {
    SceUID thid;
    int res;
    SceCtrlData pad;
    sceCtrlSetSamplingCycle(0);
    sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);
    while (1) {
        sceCtrlPeekBufferPositive(&pad, 1);
        if (pad.Buttons != 0) {
            if (pad.Buttons & PSP_CTRL_LTRIGGER) {
                res = pspSdkReferThreadStatusByName("user_main", &thid, NULL);
                res = sceKernelSuspendThread(thid);
                pspDebugScreenInit();
                pspDebugScreenPrintf("testing...");
            }
            if (pad.Buttons & PSP_CTRL_RTRIGGER) {
                res = pspSdkReferThreadStatusByName("user_main", &thid, NULL);
                res = sceKernelResumeThread(thid);
            }
        }
        sceKernelDelayThread(100000);
    }
    sceKernelExitDeleteThread(0);
    return 0;
}

int module_start(SceSize argc, void* argp) {
    SceUID thid = sceKernelCreateThread("TEST_main", main_thread, 0x30, 0x10000, 0, NULL);
    if (thid >= 0)
        sceKernelStartThread(thid, argc, argp);
    return 0;
}

Here's my Makefile:
Code: Select all
TARGET = test
OBJS = main.o

#USE_KERNEL_LIBC = 1
#USE_KERNEL_LIBS = 1

INCDIR =
CFLAGS = -O2 -G0 -Wall -fno-builtin-printf
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LIBDIR =

LIBS =

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build_prx.mak


I have to comment out USE_KERNEL_LIBC/USE_KERNEL_LIBS in the Makefile to get pspDebugScreenPrintf working. I'm not certain what these do exaclty... Will there be any negative side effects and/or is there a better why of printing text without doing that?
svanheulen
 
Posts: 33
Joined: Thu Dec 23, 2010 6:30 pm

Re: Display text in game from plugin

Postby svanheulen » Sun Dec 26, 2010 10:43 pm

Also I got the thread name "user_main" from the game I'm testing it on, Monster Hunter Portable 3rd, using psplink. I'm not sure if that's the standard main thread for all games.
svanheulen
 
Posts: 33
Joined: Thu Dec 23, 2010 6:30 pm

Re: Display text in game from plugin

Postby svanheulen » Wed Dec 29, 2010 2:49 am

Made some progress. I get the module name from the EBOOT.BIN on the UMD and from there I can get all of it's threads and suspend them. After the game has been suspended completely I can use the normal debug printing to display stuff. When I'm all done I just resume all the game's threads and everything goes back to normal. There is only one final problem I have: I can still bring up the "exit to xmb" menu when I press the home/playstation button when the game is suspended. If I select yes to exit to xmb it stalls/crashes and I have to hold down the power switch until it turns off. So, can anyone give any insight on what I need to do to either make it exit correctly or disable the home/playstation button?

I have my project up on github now if anyone is interested in the code: http://github.com/svanheulen/mhsc
svanheulen
 
Posts: 33
Joined: Thu Dec 23, 2010 6:30 pm

Re: Display text in game from plugin

Postby JJS » Wed Dec 29, 2010 8:24 am

svanheulen wrote:Also I got the thread name "user_main" from the game I'm testing it on, Monster Hunter Portable 3rd, using psplink. I'm not sure if that's the standard main thread for all games.
I think it is always called user_main.

svanheulen wrote:If I select yes to exit to xmb it stalls/crashes and I have to hold down the power switch until it turns off. So, can anyone give any insight on what I need to do to either make it exit correctly or disable the home/playstation button?
When the quit option on the exit screen is chosen, the exit callback of the game is called and the kernel waits for the game to quit (while displaying "Please wait..."). So a possibility would be to hook the exit callback of the game and handle it yourself or pass it through depending on the situation.

svanheulen wrote:I have to comment out USE_KERNEL_LIBC/USE_KERNEL_LIBS in the Makefile to get pspDebugScreenPrintf working. I'm not certain what these do exaclty... Will there be any negative side effects and/or is there a better why of printing text without doing that?
This means you are linking with newlib, which will bloat your binary quite a bit. USE_KERNEL_LIBC takes the libc implementation from the kernel, but that is incomplete. You could roll your own text printing, HBL does it too. I don't know why debug screen printing doesn't work with the kernel libc though.
JJS
Big Beholder
 
Posts: 1516
Joined: Mon Sep 27, 2010 2:18 pm

Re: Display text in game from plugin

Postby svanheulen » Wed Dec 29, 2010 9:43 am

JJS wrote:I think it is always called user_main.
That would be nice, It would make my code much simpler... so long as everything is coded correctly and the other threads don't do funky stuff while user_main is suspended, haha. There are only a limited number of games my plugin is supposed to work on so I guess I could just check them all.

JJS wrote:When the quit option on the exit screen is chosen, the exit callback of the game is called and the kernel waits for the game to quit (while displaying "Please wait..."). So a possibility would be to hook the exit callback of the game and handle it yourself or pass it through depending on the situation.
Urg, sounds like a pain. Silly me just noticed PSP_CTRL_HOME, It'll probably be fine if I just resume the thread(s) when that's pressed.

JJS wrote:This means you are linking with newlib, which will bloat your binary quite a bit. USE_KERNEL_LIBC takes the libc implementation from the kernel, but that is incomplete. You could roll your own text printing, HBL does it too. I don't know why debug screen printing doesn't work with the kernel libc though.
I actually figured out how to get debug screen stuff working with the USE_KERNEL_LIBC/LIBS. I had to add -lpspge to LIBS after importing build_prx.mak. Not sure why in that particular place but it works and my prx is 90% smaller so win-win.

Thanks for the input! Glad to feel like I'm not talking to myself, haha.
I haven't found many code examples for plugins so I've just been stumbling through it. If you want to check out my code on github and give me any feedback, I would be eternally grateful :)
svanheulen
 
Posts: 33
Joined: Thu Dec 23, 2010 6:30 pm

Re: Display text in game from plugin

Postby m0skit0 » Wed Dec 29, 2010 10:05 am

JJS wrote:I think it is always called user_main.

It's not always called user_main, IIRC. Anyway you cannot assume it will be always called user_main since it can be called anything.

The best way to do what you're asking for is hooking sceKernelCreateThread() system call.

I coded a plugin for a friend some time ago for translation, which does some hooking on the file I/O part, to replace original game files with translated ones. I think I posted the source code in Daxhordes, I'll take a look and bring it around here.

EDIT: found it, here it is. It's an older version than the one I have in my computer IIRC, but anyway it explains everything you need to know to hook system calls from a plugin. Some stuff is in spanish, although code comments are in english. Good luck ;)
Attachments
Traductor_Source.tar.gz
(2.49 KiB) Downloaded 137 times
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: Display text in game from plugin

Postby Nymphaea » Wed Dec 29, 2010 6:03 pm

In the plugins main menu loop(for when the text is on the screen), add this:
Code: Select all
while(pad.Buttons & PSP_CTRL_HOME) {
    sceKernelDelayThreadCB(1000000);
    sceCtrlReadBufferPositive(&pad,1);
}


The reason it freezes is it is waiting for a chance to run the callback, which it can't do because you kind of stopped it's threads from giving it a chance.(the PSP doesn't run CB's in it's own thread, it is run as part of your own threads)

Slight problem with this though, depending how the game is programmed, this may not work, because it might just set a flag and expect the games threads to do the rest. Best fix for that I can think of is make leaving the home screen also leave your menu, so inside that loop you can check for cross, circle or home to be pressed(all three always close the home screen I believe), and if they are close your menu and wake up the threads so they can do their thing.
There are 10 types of people in the world:
jocks,
nerds,
preps,
emos,
punks,
crazies,
losers,
ghosts,
individuals
and people who don't give a damn about the stupid binary joke.
Nymphaea
Retired Mod
 
Posts: 178
Joined: Fri Oct 01, 2010 8:40 pm

Re: Display text in game from plugin

Postby svanheulen » Thu Dec 30, 2010 3:36 am

m0skit0 wrote:EDIT: found it, here it is. It's an older version than the one I have in my computer IIRC, but anyway it explains everything you need to know to hook system calls from a plugin. Some stuff is in spanish, although code comments are in english. Good luck ;)
Thanks, interesting code! Not sure if I'm understanding it correctly, but it looks like the game has a stub for functions and the kernel points them to the correct functions. So you just point it to your own function after the kernel does it's thing? How do you find the stub address?

Nymphaea wrote:Best fix for that I can think of is make leaving the home screen also leave your menu, so inside that loop you can check for cross, circle or home to be pressed(all three always close the home screen I believe), and if they are close your menu and wake up the threads so they can do their thing.
I got it working, thanks! I just made it so that pressing the home button resumes the game and leaves my menu.
svanheulen
 
Posts: 33
Joined: Thu Dec 23, 2010 6:30 pm

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests