Skip to content

Conversation

@halx99
Copy link
Collaborator

@halx99 halx99 commented Jul 27, 2025

Describe your changes

Issue ticket number and link

Checklist before requesting a review

For each PR

  • Add Copyright if it missed:
    - "Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md)."

  • I have performed a self-review of my code.

    Optional:

    • I have checked readme and add important infos to this PR.
    • I have added/adapted some tests too.

For core/new feature PR

  • I have checked readme and add important infos to this PR.
  • I have added thorough tests.

Axmol 3.x ------------------------------------------------------------

For each 3.x PR

  • Check the '#include "axmol.h"' and replace it with the needed headers.

Replace script

$PathList = @(
    'extensions/assets-manager'
    'extensions/cocostudio'
    'extensions/GUI'
    'extensions/Inspector'
    'extensions/JSONDefault'
    'extensions/Particle3D'
    'extensions/physics-nodes'
    'extensions/scripting'
    'core'
)


function Replace-GuardInclude {
    param(
        [Parameter(Mandatory = $true)]
        [string]$Path   # 要处理的根目录
    )

    $files = Get-ChildItem -Path $Path -Recurse -Include *.h, *.hpp

    foreach ($filepath in $files) {
        # echo "Processing file $filepath"

        $lines = Get-Content $filepath -Encoding UTF8
        $lines = [System.Collections.ArrayList]$lines

        # 正则:匹配文件开头的 include guard(#ifndef/#define)
        $patternStart = '(?ms)^\s*#\s*ifndef\s+([A-Za-z_][A-Za-z0-9_]*)\s*'
        $patternDefine = '\s*#\s*define\s+'
                    
        # 正则:匹配文件结尾的 #endif(支持注释)
        $patternEnd   = '(?ms)#\s*endif(?:\s*//.*)?\s*'

        $firstIf = $null
        $firstDefine = $null
        $lastEndif = $null

        $firstIfIdx = -1
        $firstDefineIdx = -1
        $lastEndifIdx = -1

        $lineIdx = 0

        $has_pargam_once = $false
        
        foreach($lineText in $lines) {
            if ($lineText -match '^\s*#\s*pragma\s+once\b') {
                Write-Host "Skipped (has pragma once): $filepath"
                $has_pargam_once = $true
                break
            }
            elseif($lineText -match $patternStart) {
                if(!$firstIf) {
                    $firstIf = $lineText
                    $firstIfIdx = $lineIdx
                }
            }
            elseif($lineText -match $patternDefine) {
                if($firstDefineIdx -eq -1) {
                    $firstDefineIdx = $lineIdx
                    $firstDefine = $lineText
                }
            }
            elseif($lineText -match $patternEnd) {
                $lastEndif = $lineText
                $lastEndifIdx = $lineIdx
            }
            ++$lineIdx
        }

        if ($has_pargam_once) {
            continue
        }

        if($firstIfIdx -ge $firstDefineIdx) {
            throw "$firstIfIdx >= $firstDefineIdx"
        }

        if ($firstDefineIdx -ge $lastEndifIdx) {
            throw "$firstDefineIdx -ge $lastEndifIdx"
        }

        # echo "$filepath include guard info: firstIfIdx=$firstIfIdx, firstDefineIdx=$firstDefineIdx, lastEndifIdx=$lastEndifIdx, firstIf=$firstIf, lastEndif=$lastEndif"

        # step.1 remove lastEndif
        $lines.RemoveAt($lastEndifIdx)

        $lines[$firstIfIdx] = '#pragma once'
        if (-not $lines[$firstDefineIdx + 1] -match '^\s*$') {
            echo "$filepath no empty line after first define: $firstDefine"
            $lines[$firstDefineIdx] = ''
        }
        else {
            $lines.RemoveAt($firstDefineIdx)
        }

        Set-Content -Path $filepath -Value $lines -Encoding UTF8
    }

}

foreach ($Path in $PathList) {

    $Path = Join-Path $PSScriptRoot $Path

    Replace-GuardInclude $Path
}

@halx99 halx99 added this to the 2.8.0 milestone Jul 27, 2025
@halx99 halx99 changed the title Use program once for more header files Use pragma once for more header files Jul 27, 2025
@halx99 halx99 marked this pull request as draft July 27, 2025 17:26
@aismann
Copy link
Contributor

aismann commented Jul 28, 2025

I tried this:
findstr /s /r /c:"#include.*axmol" *.h

and get few files which has the "axmol.h"

@halx99
Copy link
Collaborator Author

halx99 commented Jul 28, 2025

This PR not plan to remove all axmol.h includes

@halx99 halx99 marked this pull request as ready for review July 29, 2025 16:28
@halx99 halx99 merged commit 91ee652 into dev Jul 29, 2025
29 checks passed
@halx99 halx99 deleted the use-more-pragma-once branch July 29, 2025 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants